global with sharing class issueRecieptAndInvoiceClass
{
    
    //For Generating the Invoice Button....
    WebService static string validateInvoiceAndLineItemMethod(ID accountId)
    {
        List<Invoice__c> invoiceList = new List<Invoice__c>([select Id,Account__c from Invoice__c where Account__c = : accountId AND Cancelled__c = false AND isPerformaInvoice__c =: false]);
        
        if(invoiceList.size() > 0)
            return 'Records Exist';
        else
            return 'No Record';    
            
        return null;
    }
    
    //For Generating the Release Location Method For Account...
    WebService static string releaseLocationSetupMethod(ID accountId, string processName)
    {
        Savepoint sp = Database.setSavepoint();
        try
        {
            
            //Account Query...
            Account accountObject = new Account();
            accountObject = [select Id,Location_Release_Date__c,License_Application_Type__c,License_Application_Step__c from Account where Id =: accountId];
            
            //Location List for the...
            List<Location__c> locationList = new List<Location__c>();
                
            if(processName == 'Cancellation')
            {
                List<Tenancy_Contract__c> tenancyContractUpdateList = new List<Tenancy_Contract__c>();  
               
                for(Tenancy_Contract__c tenancyObject : [Select Tenancy_Contract_Cancellation_Date__c,Tenant__c, Name, Location__c, Id, (Select Id,underCancellationFlag__c, Location_Booked_Information_Flag__c,Rental_Rate__c, Current_Tenancy_Contract__c From Locations__r) From Tenancy_Contract__c  where Tenant__c =: accountId AND Tenancy_Contract_Cancellation_Date__c = null])
                {
                    tenancyObject.Tenancy_Contract_Cancellation_Date__c = System.Today();
                    
                    System.debug('The Tenancy Contract Cancellation Date => '+ tenancyObject.Tenancy_Contract_Cancellation_Date__c);
                    //Adding in the Tenancy Contract Updation list...
                    tenancyContractUpdateList.add(tenancyObject);
                    System.debug('The Cancellation Location SIze => '+ tenancyObject);
                    
                    //Traversing the Location Associated to the Tenancy Contract...
                    for(Location__c locationObject : tenancyObject.Locations__r)
                    {
                        System.debug('The locationList Inside=> '+ locationObject);
    
                        //Previous Approach Before 4th May 2012...
                        /*locationObject.Current_Tenancy_Contract__c = null;
                        locationObject.Location_Booked_Information_Flag__c = false;*/
                        
                        //New Approach After 4th May 2012...                    
                        locationObject.underCancellationFlag__c = true;
                        //Adding in the List...
                        locationList.add(locationObject);
                        
                    }
                    
                }
                
                List<Receipt__c> receiptUpdateList = new List<Receipt__c>();
                
                //Now Update the Account Associated Receipt to make sure after this step user can't cancell this Receipt...(11/Feb/2013)
                for(Receipt__c receiptObject : [SELECT id,Company__c, Fee_Application_Type__c,isProcessConfirmed__c, CreatedDate,is_Cancelled_Receipt__c  FROM  Receipt__c where Company__c =: accountId AND Fee_Application_Type__c =: processName AND is_Cancelled_Receipt__c = false AND isProcessConfirmed__c =: false])
                {
                    receiptObject.isProcessConfirmed__c = true;
                    //Adding in the List...
                    receiptUpdateList.add(receiptObject);   
                }
                
                //Now Update the Amendment 
                if(receiptUpdateList.size() > 0)
                    update receiptUpdateList;
                    
                //For Updating the Tenancy Contract...
                if(tenancyContractUpdateList.size() > 0)
                    update tenancyContractUpdateList;
                //Updating the Location...
                if(locationList.size() > 0)
                    update locationList;    
            }
            else if(processName == 'Renew With Change Location')
            {
                //First Query to the Preivous Tenancy Contract from Account Location Object
                List<Id> accountLocationTenancyList = new List<Id>(); 
                //for(Account_Location__c accountLocationObject : [Select Account__c, isRenewed__c, Previous_Tenancy_Contract__c From Account_Location__c WHERE isRenewed__c =: false AND Previous_Tenancy_Contract__c !=: null AND Account__c =: accountId])
                for(Account_Location__c accountLocationObject : [Select Account__c, isRenewed__c, Previous_Tenancy_Contract__c From Account_Location__c WHERE isRenewed__c =: false AND New_Location__c !=: null AND Account__c =: accountId])
                {
                    accountLocationTenancyList.add(accountLocationObject.Previous_Tenancy_Contract__c);
                }
                
                List<Tenancy_Contract__c> tenancyContractUpdateList = new List<Tenancy_Contract__c>();  
               
                for(Tenancy_Contract__c tenancyObject : [Select Tenancy_Contract_Cancellation_Date__c,Tenant__c, Name, Location__c, Id, (Select Id,underCancellationFlag__c, Location_Booked_Information_Flag__c,Rental_Rate__c, Current_Tenancy_Contract__c From Locations__r) From Tenancy_Contract__c  where Id IN : accountLocationTenancyList])
                {
                    tenancyObject.Tenancy_Contract_Cancellation_Date__c = System.Today();
                    
                    System.debug('The Tenancy Contract Cancellation Date => '+ tenancyObject.Tenancy_Contract_Cancellation_Date__c);
                    //Adding in the Tenancy Contract Updation list...
                    tenancyContractUpdateList.add(tenancyObject);
                    System.debug('The Cancellation Location SIze => '+ tenancyObject);
                    
                    //Traversing the Location Associated to the Tenancy Contract...
                    for(Location__c locationObject : tenancyObject.Locations__r)
                    {
                        System.debug('The locationList Inside=> '+ locationObject);
    
                        //Previous Approach Before 4th May 2012...
                        /*locationObject.Current_Tenancy_Contract__c = null;
                        locationObject.Location_Booked_Information_Flag__c = false;*/
                        
                        //New Approach After 4th May 2012...                    
                        locationObject.underCancellationFlag__c = true;
                        //Adding in the List...
                        locationList.add(locationObject);
                        
                    }
                    
                }
                //For Updating the Tenancy Contract...
                if(tenancyContractUpdateList.size() > 0)
                    update tenancyContractUpdateList;
                //Updating the Location...
                if(locationList.size() > 0)
                    update locationList;    
                    
            }
            
                
            //Now Update the status...
            accountObject.License_Application_Step__c = 'Location Release';
            accountobject.Location_Release_Date__c = system.today();
            //Now Update the Account Object...
            update accountObject;
            
            //Now Returning the Values...
            return 'Success';
            
        }
        catch(System.Exception exp)
        {
            System.debug('The Error is => '+ exp.getMessage());
            Database.rollback(sp);
            return null;
        
        }
        
    }
    
    //For Generating the Release Location MEthod For AmendmentObject...
    WebService static string releaseLocationSetupForAmendmentMethod(ID accountId,ID amendmentId,ID previousTenancyContract,ID previousTenancyContract2,ID previousTenancyContract3)
    {
        Savepoint sp = Database.setSavepoint();
        try
        {
            
            //Amendment Query...
            Amendment__c amendmentObject = new Amendment__c();
            amendmentObject = [select Id,Previous_Tenancy_Location_Release_Date__c,Amendment_Application_Status__c from Amendment__c where Id =: amendmentId AND Account__c =: accountId];
            //Location List for the...
            List<Location__c> locationList = new List<Location__c>();
            List<Tenancy_Contract__c> tenancyContractUpdateList = new List<Tenancy_Contract__c>();  
            for(Tenancy_Contract__c tenancyObject : [Select Tenant__c, Name, Location__c, Id, (Select Id,underCancellationFlag__c, Location_Booked_Information_Flag__c,Rental_Rate__c, Current_Tenancy_Contract__c From Locations__r) From Tenancy_Contract__c  where Tenant__c =: accountId AND (Id =: previousTenancyContract OR Id =: previousTenancyContract2 OR Id =: previousTenancyContract3)])
            {
            
                tenancyObject.Tenancy_Contract_Cancellation_Date__c= System.Today();
                //Adding in the Tenancy Contract Updation list...
                tenancyContractUpdateList.add(tenancyObject);
                
                //Traversing the Location Associated to the Tenancy Contract...
                for(Location__c locationObject : tenancyObject.Locations__r)
                {
                    //Previous Approach Before 4th May 2012...
                    /*locationObject.Current_Tenancy_Contract__c = null;
                    locationObject.Location_Booked_Information_Flag__c = false;*/
                    
                    //New Approach After 4th May 2012...
                    locationObject.underCancellationFlag__c = true;
                    
                    //Adding in the List...
                    locationList.add(locationObject);
                    
                }
                
            }
            
            //For Updating the Tenancy Contract...
            if(tenancyContractUpdateList.size() > 0)
                update tenancyContractUpdateList;
            //Updating the Location...
            if(locationList.size() > 0)
                update locationList;
            
            //Now Update the status...
            amendmentObject.Amendment_Application_Status__c = 'Location Release';
            amendmentObject.Previous_Tenancy_Location_Release_Date__c = System.today();
            //Now Update the Amendment Object...
            update amendmentObject;
            
            //Now Returning the Values...
            return 'Success';
            
        }
        catch(System.Exception exp)
        {
            System.debug('The Error is => '+ exp.getMessage());
            Database.rollback(sp);
            return null;
        
        }
        
    }
    
    //For Generating the Tenancy Contract For Amendment Object...
    WebService static List<string> generateTenancyContractForAmendmentMethod(ID accountId,ID amendmentId,string recordTypeName, ID newLocationId,string accountExpiryDate)
    {
        Savepoint sp = Database.setSavepoint();
        try
        {
            List<string> resultList = new List<string>();
            //Amendment Query...
            Amendment__c amendmentObject = new Amendment__c();
            amendmentObject = [select Id,NTCDI__c,NLPPD__c,NLPPD_2__c,NLPPD_3__c,OTCDL__c,PTCPPD__c,New_Tenancy_Contract_Price_Calculation_1__c,New_Location_2__c,New_Location_2__r.Id,New_Location_3__c,New_Location_3__r.Id,New_Location__c,New_Location__r.Id,New_Tenancy_Contract__c,Previous_Tenancy_Location_Release_Date__c,Amendment_Application_Status__c from Amendment__c where Id =: amendmentId AND Account__c =: accountId];
            RecordType tenancyRecordType = [select Id,DeveloperName,SObjectType,IsActive from RecordType where SObjectType =: 'Tenancy_Contract__c' AND IsActive =: true AND DeveloperName =: 'New_Tenancy_Contract' ];
            
            //Location List for the...
            List<Tenancy_Contract__c> tenancyObjectList = new List<Tenancy_Contract__c>();
            
            Map<ID,Invoice_Line_Item__c> invoiceItemMap = new Map<Id,Invoice_Line_Item__c>();
            
            //Invoice Always One...
            //for(Invoice__c invoiceObject : [Select Invoice_Type__c, Id, Fee_Application_Type__c, Cancelled__c, Account__c,CreatedDate, (Select Id, Name, Invoice__c, Amount__c, Fee__c, Location_Booking_Information__c,Location_Booking_Information__r.id, Location__c, Package_Value__c, Action__c, Installment__c,Tenancy_Contract__c,Location__r.Id,Tenancy_Contract__r.Id, Payment_Plan__c From Invoice_Line_Items__r WHERE Name =: 'Location Rental Rate') From Invoice__c WHERE Account__c =: accountId AND Invoice_Type__c =: 'Non Refundable' AND Fee_Application_Type__c =: 'Amendment' AND Cancelled__c =: false ORDER BY CreatedDate DESC LIMIT 1])
            for(Invoice__c invoiceObject : [Select Invoice_Type__c, Id, Fee_Application_Type__c, Cancelled__c, Account__c,CreatedDate, (Select Id, Name, Invoice__c,Start_Date__c,End_Date__c, Amount__c, Fee__c, Location_Booking_Information__c,Location_Booking_Information__r.id, Location__c, Package_Value__c, Action__c, Installment__c,Tenancy_Contract__c,Location__r.Id,Tenancy_Contract__r.Id, Payment_Plan__c From Invoice_Line_Items__r WHERE Name LIKE: 'Location Rental Rate%') From Invoice__c WHERE Account__c =: accountId AND Invoice_Type__c =: 'Non Refundable' AND Fee_Application_Type__c =: 'Amendment' AND Cancelled__c =: false ORDER BY CreatedDate DESC LIMIT 1])
            {
                for(Invoice_Line_Item__c invoiceItem : invoiceObject.Invoice_Line_Items__r )
                {
                    //Putting in the Map...
                    invoiceItemMap.put(invoiceItem.Location__r.id, invoiceItem);
                }   
            }
            
            if(recordTypeName == 'Location Amendment - Add Location')
            {
                if(amendmentObject.New_Location__c != null)
                {
                    //Adding the Tenancy Contract in salesforce....
                    Tenancy_Contract__c tenancyObject = new Tenancy_Contract__c();
                    tenancyObject.Tenant__c = accountId;
                    tenancyObject.RecordTypeId = tenancyRecordType.id;
                    tenancyObject.Tenancy_Contract_Start_Date__c = system.today();
                    if(system.today() < date.parse(accountExpiryDate))
                        tenancyObject.Tenancy_Contract_Expiry_Date__c = date.parse(accountExpiryDate);
                    else
                    {
                        System.debug('The Else Condition for Tenancy Contract => '+ date.parse(accountExpiryDate).addyears(1));
                        tenancyObject.Tenancy_Contract_Expiry_Date__c = date.parse(accountExpiryDate).addyears(1);
                    }   
                    //Package Selection...
                    if(invoiceItemMap.get(amendmentObject.New_Location__r.Id) != null)
                    {
                        tenancyObject.Rental_Rate__c =invoiceItemMap.get(amendmentObject.New_Location__r.Id).Amount__c;
                        tenancyObject.Package_Value__c = invoiceItemMap.get(amendmentObject.New_Location__r.Id).Package_Value__c;
                        tenancyObject.Payment_Plan__c = invoiceItemMap.get(amendmentObject.New_Location__r.Id).Payment_Plan__c;
                        tenancyObject.Action__c = invoiceItemMap.get(amendmentObject.New_Location__r.Id).Action__c;
                        tenancyObject.Installment__c = invoiceItemMap.get(amendmentObject.New_Location__r.Id).Installment__c;
                        
                        //Adding on 25th Nov 2013..
                        tenancyObject.Tenancy_Contract_Start_Date__c = invoiceItemMap.get(amendmentObject.New_Location__r.Id).Start_Date__c;
                        tenancyObject.Tenancy_Contract_Expiry_Date__c = invoiceItemMap.get(amendmentObject.New_Location__r.Id).End_Date__c;
                    }
                    else
                    {
                        tenancyObject.Rental_Rate__c = amendmentObject.NLPPD__c * amendmentObject.NTCDI__c;
                    }
                    
                    tenancyObject.Location__c = amendmentObject.New_Location__c;
                    tenancyObject.Amendment__c = amendmentObject.Id;
                    
                    
                    //Add in the List...
                    tenancyObjectList.add(tenancyObject);
                }
                if(amendmentObject.New_Location_2__c != null)
                {
                    //Adding the Tenancy Contract in salesforce....
                    Tenancy_Contract__c tenancyObject = new Tenancy_Contract__c();
                    tenancyObject.Tenant__c = accountId;
                    tenancyObject.RecordTypeId = tenancyRecordType.id;
                    tenancyObject.Tenancy_Contract_Start_Date__c = system.today();
                    tenancyObject.Tenancy_Contract_Expiry_Date__c = date.parse(accountExpiryDate);
                    
                    //Package Selection...
                    if(invoiceItemMap.get(amendmentObject.New_Location_2__r.Id) != null)
                    {
                        tenancyObject.Rental_Rate__c =invoiceItemMap.get(amendmentObject.New_Location_2__r.Id).Amount__c;
                        tenancyObject.Package_Value__c = invoiceItemMap.get(amendmentObject.New_Location_2__r.Id).Package_Value__c;
                        tenancyObject.Payment_Plan__c = invoiceItemMap.get(amendmentObject.New_Location_2__r.Id).Payment_Plan__c;
                        tenancyObject.Action__c = invoiceItemMap.get(amendmentObject.New_Location_2__r.Id).Action__c;
                        tenancyObject.Installment__c = invoiceItemMap.get(amendmentObject.New_Location_2__r.Id).Installment__c;
                        
                        //Adding on 25th Nov 2013..
                        tenancyObject.Tenancy_Contract_Start_Date__c = invoiceItemMap.get(amendmentObject.New_Location_2__r.Id).Start_Date__c;
                        tenancyObject.Tenancy_Contract_Expiry_Date__c = invoiceItemMap.get(amendmentObject.New_Location_2__r.Id).End_Date__c;
                        
                    }
                    else
                    {
                        tenancyObject.Rental_Rate__c = amendmentObject.NLPPD_2__c * amendmentObject.NTCDI__c;   
                    }
                    
                    tenancyObject.Location__c = amendmentObject.New_Location_2__c;
                    tenancyObject.Amendment__c = amendmentObject.Id;
                    //Add in the List...
                    tenancyObjectList.add(tenancyObject);
                }
                if(amendmentObject.New_Location_3__c != null)
                {
                    //Adding the Tenancy Contract in salesforce....
                    Tenancy_Contract__c tenancyObject = new Tenancy_Contract__c();
                    tenancyObject.Tenant__c = accountId;
                    tenancyObject.RecordTypeId = tenancyRecordType.id;
                    tenancyObject.Tenancy_Contract_Start_Date__c = system.today();
                    tenancyObject.Tenancy_Contract_Expiry_Date__c = date.parse(accountExpiryDate);
                    
                    //Package Selection...
                    if(invoiceItemMap.get(amendmentObject.New_Location_3__r.Id) != null)
                    {
                        tenancyObject.Rental_Rate__c =invoiceItemMap.get(amendmentObject.New_Location_3__r.Id).Amount__c;
                        tenancyObject.Package_Value__c = invoiceItemMap.get(amendmentObject.New_Location_3__r.Id).Package_Value__c;
                        tenancyObject.Payment_Plan__c = invoiceItemMap.get(amendmentObject.New_Location_3__r.Id).Payment_Plan__c;
                        tenancyObject.Action__c = invoiceItemMap.get(amendmentObject.New_Location_3__r.Id).Action__c;
                        tenancyObject.Installment__c = invoiceItemMap.get(amendmentObject.New_Location_3__r.Id).Installment__c;
                        
                        //Adding on 25th Nov 2013..
                        tenancyObject.Tenancy_Contract_Start_Date__c = invoiceItemMap.get(amendmentObject.New_Location_3__r.Id).Start_Date__c;
                        tenancyObject.Tenancy_Contract_Expiry_Date__c = invoiceItemMap.get(amendmentObject.New_Location_3__r.Id).End_Date__c;
                    }
                    else
                    {
                        tenancyObject.Rental_Rate__c = amendmentObject.NLPPD_3__c * amendmentObject.NTCDI__c;   
                    }
                    
                    tenancyObject.Location__c = amendmentObject.New_Location_3__c;
                    tenancyObject.Amendment__c = amendmentObject.Id;
                    //Add in the List...
                    tenancyObjectList.add(tenancyObject);
                }
            
            }
            else if(recordTypeName == 'Location Amendment - Change Location')
            {
                //Adding the Tenancy Contract in salesforce....
                Tenancy_Contract__c tenancyObject = new Tenancy_Contract__c();
                tenancyObject.Tenant__c = accountId;
                tenancyObject.RecordTypeId = tenancyRecordType.id;
                tenancyObject.Tenancy_Contract_Start_Date__c = system.today();
                tenancyObject.Tenancy_Contract_Expiry_Date__c = date.parse(accountExpiryDate);
                //Package Selection...
                if(invoiceItemMap.get(amendmentObject.New_Location__r.Id) != null)
                {
                    tenancyObject.Rental_Rate__c =invoiceItemMap.get(amendmentObject.New_Location__r.Id).Amount__c;
                    tenancyObject.Package_Value__c = invoiceItemMap.get(amendmentObject.New_Location__r.Id).Package_Value__c;
                    tenancyObject.Payment_Plan__c = invoiceItemMap.get(amendmentObject.New_Location__r.Id).Payment_Plan__c;
                    tenancyObject.Action__c = invoiceItemMap.get(amendmentObject.New_Location__r.Id).Action__c;
                    tenancyObject.Installment__c = invoiceItemMap.get(amendmentObject.New_Location__r.Id).Installment__c;
                    
                    //Adding on 25th Nov 2013..
                        tenancyObject.Tenancy_Contract_Start_Date__c = invoiceItemMap.get(amendmentObject.New_Location__r.Id).Start_Date__c;
                        tenancyObject.Tenancy_Contract_Expiry_Date__c = invoiceItemMap.get(amendmentObject.New_Location__r.Id).End_Date__c;
                    
                }
                else
                {
                    //This is incorrect as per the Invoice Item...
                    //tenancyObject.Rental_Rate__c = amendmentObject.New_Tenancy_Contract_Price_Calculation_1__c;
                    tenancyObject.Rental_Rate__c = ((amendmentObject.NLPPD__c * amendmentObject.NTCDI__c) - (amendmentObject.PTCPPD__c * amendmentObject.OTCDL__c));        
                }
                
                tenancyObject.Location__c = amendmentObject.New_Location__c;
                tenancyObject.Amendment__c = amendmentObject.Id;
                //Add in the List...
                tenancyObjectList.add(tenancyObject);
            }
            
            
            //Before 16th October 2012 ... (Package Implementation)
            /*if(recordTypeName == 'Location Amendment - Add Location')
            {
                if(amendmentObject.New_Location__c != null)
                {
                    //Adding the Tenancy Contract in salesforce....
                    Tenancy_Contract__c tenancyObject = new Tenancy_Contract__c();
                    tenancyObject.Tenant__c = accountId;
                    tenancyObject.RecordTypeId = tenancyRecordType.id;
                    tenancyObject.Tenancy_Contract_Start_Date__c = system.today();
                    tenancyObject.Tenancy_Contract_Expiry_Date__c = date.parse(accountExpiryDate);
                    tenancyObject.Rental_Rate__c = amendmentObject.NLPPD__c * amendmentObject.NTCDI__c;
                    tenancyObject.Location__c = amendmentObject.New_Location__c;
                    tenancyObject.Amendment__c = amendmentObject.Id;
                    //Add in the List...
                    tenancyObjectList.add(tenancyObject);
                }
                if(amendmentObject.New_Location_2__c != null)
                {
                    //Adding the Tenancy Contract in salesforce....
                    Tenancy_Contract__c tenancyObject = new Tenancy_Contract__c();
                    tenancyObject.Tenant__c = accountId;
                    tenancyObject.RecordTypeId = tenancyRecordType.id;
                    tenancyObject.Tenancy_Contract_Start_Date__c = system.today();
                    tenancyObject.Tenancy_Contract_Expiry_Date__c = date.parse(accountExpiryDate);
                    tenancyObject.Rental_Rate__c = amendmentObject.NLPPD_2__c * amendmentObject.NTCDI__c;
                    tenancyObject.Location__c = amendmentObject.New_Location_2__c;
                    tenancyObject.Amendment__c = amendmentObject.Id;
                    //Add in the List...
                    tenancyObjectList.add(tenancyObject);
                }
                if(amendmentObject.New_Location_3__c != null)
                {
                    //Adding the Tenancy Contract in salesforce....
                    Tenancy_Contract__c tenancyObject = new Tenancy_Contract__c();
                    tenancyObject.Tenant__c = accountId;
                    tenancyObject.RecordTypeId = tenancyRecordType.id;
                    tenancyObject.Tenancy_Contract_Start_Date__c = system.today();
                    tenancyObject.Tenancy_Contract_Expiry_Date__c = date.parse(accountExpiryDate);
                    tenancyObject.Rental_Rate__c = amendmentObject.NLPPD_3__c * amendmentObject.NTCDI__c;
                    tenancyObject.Location__c = amendmentObject.New_Location_3__c;
                    tenancyObject.Amendment__c = amendmentObject.Id;
                    //Add in the List...
                    tenancyObjectList.add(tenancyObject);
                }
                
            
            }
            else if(recordTypeName == 'Location Amendment - Change Location')
            {
                //Adding the Tenancy Contract in salesforce....
                Tenancy_Contract__c tenancyObject = new Tenancy_Contract__c();
                tenancyObject.Tenant__c = accountId;
                tenancyObject.RecordTypeId = tenancyRecordType.id;
                tenancyObject.Tenancy_Contract_Start_Date__c = system.today();
                tenancyObject.Tenancy_Contract_Expiry_Date__c = date.parse(accountExpiryDate);
                tenancyObject.Rental_Rate__c = amendmentObject.New_Tenancy_Contract_Price_Calculation_1__c;
                tenancyObject.Location__c = amendmentObject.New_Location__c;
                tenancyObject.Amendment__c = amendmentObject.Id;
                //Add in the List...
                tenancyObjectList.add(tenancyObject);
            }
            */
            //Updating the Location...
            if(tenancyObjectList.size() > 0)
                insert tenancyObjectList;
            
            List<Location__c> locationList = new List<Location__c>();
                         
            //Traversing the Location Associated to the Tenancy Contract...
            for(Location__c locationObject :[select Id,Current_Tenancy_Contract__c,Location_Booked_Information_Flag__c from Location__c where Id =: amendmentObject.New_Location__c OR Id =: amendmentObject.New_Location_2__c OR Id =: amendmentObject.New_Location_3__c])
            {
                
                //locationObject.Current_Tenancy_Contract__c = null;
                locationObject.Location_Booked_Information_Flag__c = false;
                
                //Adding in the List...
                locationList.add(locationObject);
                
            }
            
            //Now Update the Amendment...
            amendmentObject.Amendment_Application_Status__c = 'Tenancy Contract Issued';
            //Copy the Tenancy Contract Id in Amendment Object...
            for(integer i=0; i<tenancyObjectList.size();i++)
            {
                //For Result List...
                resultList.add(tenancyObjectList[i].id);
                if(i == 0)
                    amendmentObject.New_Tenancy_Contract__c = tenancyObjectList[i].id;
                
                For(Location__c Location : locationList)
                {
                    if(tenancyObjectList[i].Location__c == Location.id)
                    {
                        
                        Location.Current_Tenancy_Contract__c = tenancyObjectList[i].id;
                        //Change the Code...
                        //Location.Location_Booked_Information_Flag__c = true;
                        break;
                    }
                }   
            }
            
            //Now Update the Locations...
            if(locationList.size() > 0)
                update locationList;
                
            //Now Update the Amendment Object...
            update amendmentObject;
            
            
            List<Receipt__c> receiptUpdateList = new List<Receipt__c>();
            
            //Now Update the Amendment Associated Receipt to make sure after this step user can't cancell this Receipt...(11/Feb/2013)
            for(Receipt__c receiptObject : [SELECT id, isProcessConfirmed__c, CreatedDate,is_Cancelled_Receipt__c  FROM Receipt__c where Amendment__c =: amendmentId AND is_Cancelled_Receipt__c = false AND isProcessConfirmed__c =: false])
            {
                receiptObject.isProcessConfirmed__c = true;
                //Adding in the List...
                receiptUpdateList.add(receiptObject);   
            }
            
            //Now Update the Amendment Associated Receipt ... 
            if(receiptUpdateList.size() > 0)
                update receiptUpdateList;
            
                    
            //Now Returning the Values...
            return resultList;
            
        }
        catch(System.Exception exp)
        {
            System.debug('The Error is => '+ exp.getMessage());
            Database.rollback(sp);
            return null;
        
        }
        
    }
    
    
    //For Generating the Invoice Button....
    //WebService static string validateReceiptCreationMethod(ID accountId)
    WebService static List<string> validateReceiptCreationMethod(ID accountId,string applicationType,string ObjectName,string objectID)
    {
        
        try
        {
            List<Invoice__c> invoiceList = new List<Invoice__c>();
            if(objectName == 'Account')
                invoiceList = new List<Invoice__c>([select Name,Tracking__c, Tenancy_Contract__c, Application__c, Amendment__c,Cancelled__c,Receipt_Generated__c,Id,CreatedDate,Account__c from Invoice__c where Account__c = : accountId AND Cancelled__c =: false AND Fee_Application_Type__c =: applicationType AND CreatedDate >= YESTERDAY AND isPerformaInvoice__c =: false ORDER BY CreatedDate desc]);
            else if(objectName == 'Amendment')
                invoiceList = new List<Invoice__c>([select Name,Tracking__c, Tenancy_Contract__c, Application__c, Amendment__c,Cancelled__c,Receipt_Generated__c,Id,CreatedDate,Account__c from Invoice__c where Account__c = : accountId AND Amendment__c =: objectID AND Cancelled__c =: false AND Fee_Application_Type__c =: applicationType AND CreatedDate >= YESTERDAY AND isPerformaInvoice__c =: false ORDER BY CreatedDate desc]);
            else if (objectName == 'Tenancy')
                invoiceList = new List<Invoice__c>([select Name,Tracking__c, Tenancy_Contract__c, Application__c, Amendment__c,Cancelled__c,Receipt_Generated__c,Id,CreatedDate,Account__c from Invoice__c where Account__c = : accountId AND Tenancy_Contract__c =:objectID AND Cancelled__c =: false AND Fee_Application_Type__c =: applicationType AND CreatedDate >= YESTERDAY AND isPerformaInvoice__c =: false ORDER BY CreatedDate desc]);
            else if(objectName == 'Tracking')   
                invoiceList = new List<Invoice__c>([select Name,Tracking__c, Tenancy_Contract__c, Application__c, Amendment__c,Cancelled__c,Receipt_Generated__c,Id,CreatedDate,Account__c from Invoice__c where Account__c = : accountId AND Tracking__c =: objectID AND Cancelled__c =: false AND Fee_Application_Type__c =: applicationType AND CreatedDate >= YESTERDAY AND isPerformaInvoice__c =: false ORDER BY CreatedDate desc]);  
            else if(objectName == 'Application')    
                invoiceList = new List<Invoice__c>([select Name,Tracking__c, Tenancy_Contract__c, Application__c, Amendment__c,Cancelled__c,Receipt_Generated__c,Id,CreatedDate,Account__c from Invoice__c where Account__c = : accountId AND Application__c =:objectID AND Cancelled__c =: false AND Fee_Application_Type__c =: applicationType AND CreatedDate >= YESTERDAY AND isPerformaInvoice__c =: false ORDER BY CreatedDate desc]);    
            else if(objectName == 'Service Letter')    
                invoiceList = new List<Invoice__c>([select Name,Tracking__c, Tenancy_Contract__c, Application__c, Amendment__c,Cancelled__c,Receipt_Generated__c,Id,CreatedDate,Account__c from Invoice__c where Account__c = : accountId AND Request__c =:objectID AND Cancelled__c =: false AND Fee_Application_Type__c =: applicationType AND CreatedDate >= YESTERDAY AND isPerformaInvoice__c =: false ORDER BY CreatedDate desc]);
            
            
            List<string> invoiceIdList = new List<string>();
            //Extracting the Invoice Ids...
            for(Invoice__c invoiceObject : invoiceList)
            {
                System.debug('The Invoice Receipt Generated Flag => '+ invoiceObject.Receipt_Generated__c);
                if(invoiceObject.Receipt_Generated__c != true)
                    invoiceIdList.add(invoiceObject.Id);
                System.debug('The Invoice Validation => '+invoiceObject.Name);  
            }
            
            System.debug('The Invoice List Size => '+invoiceList.size());
            System.debug('The Invoice Ids List Size => '+invoiceIdList.size());
            //Return the Invoice Id...
            if(invoiceList.size() > 0)
            {
                for(Receipt__c receiptObject : [select Id,name,Related_Invoice__c from Receipt__c where Related_Invoice__c IN: invoiceList ORDER BY CreatedDate desc])
                {
                    invoiceIdList.add('Receipt-'+receiptObject.name);
                    
                }
                
                //Now Return the Invoice...
                return invoiceIdList;
                
            }
            else
                return null;           
            
            
            
            /*
            List<Invoice__c> invoiceList = new List<Invoice__c>([select Name,Cancelled__c,Receipt_Generated__c,Id,CreatedDate,Account__c from Invoice__c where Account__c = : accountId AND Cancelled__c =: false AND Fee_Application_Type__c =: applicationType AND CreatedDate >= YESTERDAY AND isPerformaInvoice__c =: false ORDER BY CreatedDate desc]);
            
            List<string> invoiceIdList = new List<string>();
            //Extracting the Invoice Ids...
            for(Invoice__c invoiceObject : invoiceList)
            {
                System.debug('The Invoice Receipt Generated Flag => '+ invoiceObject.Receipt_Generated__c);
                if(invoiceObject.Receipt_Generated__c != true)
                    invoiceIdList.add(invoiceObject.Id);
                System.debug('The Invoice Validation => '+invoiceObject.Name);  
            }
            
            System.debug('The Invoice List Size => '+invoiceList.size());
            System.debug('The Invoice Ids List Size => '+invoiceIdList.size());
            //Return the Invoice Id...
            if(invoiceList.size() > 0)
            {
                for(Receipt__c receiptObject : [select Id,name,Related_Invoice__c from Receipt__c where Related_Invoice__c IN: invoiceList ORDER BY CreatedDate desc])
                {
                    invoiceIdList.add('Receipt-'+receiptObject.name);
                    
                }
                
                //Now Return the Invoice...
                return invoiceIdList;
                
            }
            else
                return null;           
            
            */
        
        }
        catch(Exception exp)
        {
            
            System.debug('The Exception is => '+ exp.getMessage());
            return null;
        }
        
        return null;    
    }
    
    //Generating the Invoice and Invoice Line Item for Service Letter....
    WebService static List<string> generatingInvoiceAndLineItemForServiceLetterMethod(ID accountId,ID serviceLetterId, string letterType,string isPerformaInvoice)
    {
        Savepoint sp = Database.setSavepoint();
        
        //Query to the Service Price Book For Invoice and Invoice Line Item...
        List<Service_Price_Book__c> servicePriceBookList = new List<Service_Price_Book__c>([Select Employment_ID_Card__c,AFZA_Employment_ID_Card__c,Advertisement_Fee__c,AFZA_Advertisement_Fee__c,AFZA_Establishment_Card__c,AFZA_Etisalat__c,AFZA_General_Services__c,AFZA_Google__c,AFZA_License_Activity_Fee__c,License_Activity_Fee__c,AFZA_Maintenance__c,AFZA_Sign_Board__c,AFZA_Sewage__c,General_Services__c, Establishment_Card__c, Etisalat__c, Google__c, Gov_Establishment_Card__c, Maintenance__c, Sewage__c, Sign_Board__c,Name,AFZA_Charges__c, AFZA_Other_Charges__c,  EMPost_Charges__c, Local_Government_Charges__c, License_Application_Type__c,Fee_Type__c,License_App_Category__c, Id, Fee__r.Fee_ID__c,Visa_Category__c, Fee__r.Fee_Type__c, Fee__r.Receipt_Group__c, Fee__r.Price__c, Fee__r.Name, Fee__c, Description__c From Service_Price_Book__c where  License_Application_Type__c =: 'Service Letter' AND Letter_Type__c =: letterType AND isActive__c =: true]);
        List<Fines__c> suspensionInvoiceItemList = new List<Fines__c>([Select Id,Account__c,Status__c, Name,Fines_Included__c, Fine_Total_Amount_Due__c From Fines__c where Fines_Included__c = true and Status__c = 'Approved' AND Account__c =: accountId]); 
        
        //Variables...
        Map<string,Invoice__c> invoiceObjectMap = new Map<string,Invoice__c>();
        Invoice_Line_Item__c tempInvoiceItem = null;
        List<Invoice_Line_Item__c> invoiceLineItemList = new List<Invoice_Line_Item__c>();
        list<String> resultList = new list<string>();
        
        try
        {
            //Create a Invoice Object.... For Non Refundable...
            Invoice__c tempInvoice = new Invoice__c();
            tempInvoice.Account__c = accountId;
            tempInvoice.Request__c  = serviceLetterId;
            tempInvoice.Date__c = System.Today();
            tempInvoice.Invoice_Type__c = 'Non Refundable';
            tempInvoice.Fee_Application_Type__c = 'Service Letter';
            
            if(isPerformaInvoice == 'True')
                tempInvoice.isPerformaInvoice__c = true;
            
            //Adding in Map....
            invoiceObjectMap.put('Non Refundable',tempInvoice); 
            
            //Create a Invoice Object.... For Guarantee...
            Invoice__c tempInvoice1 = new Invoice__c();
            tempInvoice1.Account__c = accountId;
            tempInvoice1.Request__c  = serviceLetterId;
            tempInvoice1.Date__c = System.Today();
            tempInvoice1.Invoice_Type__c = 'Guarantee';
            tempInvoice1.Fee_Application_Type__c = 'Service Letter';
            
            if(isPerformaInvoice == 'True')
                tempInvoice.isPerformaInvoice__c = true;
                
            //Adding in Map....
            invoiceObjectMap.put('Guarantee',tempInvoice1); 
            
            //For Guarantee Flag...
            Boolean guaranteeInvoiceFlag = false;
            
            
            //From Suspension Invoice Items....
            if(suspensionInvoiceItemList.size() > 0)
            {
                for(Fines__c suspensionObject : suspensionInvoiceItemList)
                {
                    //We only neeed to put the Invoice Line Item...
                    tempInvoiceItem = new Invoice_Line_Item__c();
                    
                    //Mapping the Fields...
                    tempInvoiceItem.Name = suspensionObject.Name;
                    tempInvoiceItem.Amount__c = suspensionObject.Fine_Total_Amount_Due__c;
                    tempInvoiceItem.Suspension__c = suspensionObject.Id;
                    
                    //Now Add the Invoice Item...
                    invoiceLineItemList.add(tempInvoiceItem);
                    
                }
            }
            
                    
            //For Service Price Book....
            If(servicePriceBookList.size() > 0)
            {
                for(Service_Price_Book__c servicePriceBookObject : servicePriceBookList )
                {
                    if(servicePriceBookObject.Fee__r.Fee_Type__c == 'Guarantee')
                        guaranteeInvoiceFlag = true;
                    
                    //We only neeed to put the Invoice Line Item...
                    tempInvoiceItem = new Invoice_Line_Item__c();
                    
                    //Mapping the Fields...
                    tempInvoiceItem.Name = servicePriceBookObject.Fee__r.Name;
                    tempInvoiceItem.Fee__c = servicePriceBookObject.Fee__r.Id;
                    tempInvoiceItem.Fee_Type__c = servicePriceBookObject.Fee__r.Fee_Type__c;
                    tempInvoiceItem.Amount__c = servicePriceBookObject.Fee__r.Price__c;
                    tempInvoiceItem.Service_Price_Book__c = servicePriceBookObject.Id;
                    
                    tempInvoiceItem.AFZA_Charges__c = servicePriceBookObject.AFZA_Charges__c;
                    tempInvoiceItem.AFZA_Other_Charges__c = servicePriceBookObject.AFZA_Other_Charges__c;
                    tempInvoiceItem.EMPost_Charges__c = servicePriceBookObject.EMPost_Charges__c;
                    tempInvoiceItem.Local_Government_Charges__c = servicePriceBookObject.Local_Government_Charges__c;
                    
                    tempInvoiceItem.Maintenance__c = servicePriceBookObject.Maintenance__c;
                    tempInvoiceItem.Establishment_Card__c = servicePriceBookObject.Establishment_Card__c;
                    tempInvoiceItem.Etisalat__c = servicePriceBookObject.Etisalat__c;
                    tempInvoiceItem.Google__c = servicePriceBookObject.Google__c;
                    tempInvoiceItem.Gov_Establishment_Card__c = servicePriceBookObject.Gov_Establishment_Card__c;
                    tempInvoiceItem.Sewage__c = servicePriceBookObject.Sewage__c;
                    tempInvoiceItem.Sign_Board__c = servicePriceBookObject.Sign_Board__c;
                    tempInvoiceItem.General_Services__c = servicePriceBookObject.General_Services__c;
                    
                    tempInvoiceItem.AFZA_Maintenance__c = servicePriceBookObject.AFZA_Maintenance__c;
                    tempInvoiceItem.AFZA_Establishment_Card__c = servicePriceBookObject.AFZA_Establishment_Card__c;
                    tempInvoiceItem.AFZA_Etisalat__c = servicePriceBookObject.AFZA_Etisalat__c;
                    tempInvoiceItem.AFZA_Google__c = servicePriceBookObject.AFZA_Google__c;
                    tempInvoiceItem.AFZA_Sewage__c = servicePriceBookObject.AFZA_Sewage__c;
                    tempInvoiceItem.AFZA_Sign_Board__c = servicePriceBookObject.AFZA_Sign_Board__c;
                    tempInvoiceItem.AFZA_General_Services__c = servicePriceBookObject.AFZA_General_Services__c;
                    
                    tempInvoiceItem.Advertisement_Fee__c = servicePriceBookObject.Advertisement_Fee__c;
                    tempInvoiceItem.AFZA_Advertisement_Fee__c = servicePriceBookObject.AFZA_Advertisement_Fee__c;
                    tempInvoiceItem.License_Activity_Fee__c = servicePriceBookObject.License_Activity_Fee__c;
                    tempInvoiceItem.AFZA_License_Activity_Fee__c = servicePriceBookObject.AFZA_License_Activity_Fee__c;
                    
                    tempInvoiceItem.Employment_ID_Card__c = servicePriceBookObject.Employment_ID_Card__c;
                    tempInvoiceItem.AFZA_Employment_ID_Card__c = servicePriceBookObject.AFZA_Employment_ID_Card__c;
                    
                    
                    
                    //Now Add the Invoice Item...
                    invoiceLineItemList.add(tempInvoiceItem);
                    
                    
                }
            
            }

            //Now create the Invoice records...
            if(invoiceObjectMap.size() > 0)
            {
                
                //For Guarantee Invoice Remove if its not in placed...
                if(guaranteeInvoiceFlag != true)
                    invoiceObjectMap.remove('Guarantee');
                System.debug('The Invoice Object Map Size => '+ invoiceObjectMap.size());
                //First Insert the invoice...
                insert invoiceObjectMap.values();
                
                for(Invoice__c tempInvoiceObject : invoiceObjectMap.values())
                {
                    //Putting in the Result List...
                    resultList.add(tempInvoiceObject.Id);
                    
                    //Set the Invoice Id in the Invoice Line Item...
                    if(invoiceLineItemList.size()> 0)
                    {
                        for(Invoice_Line_Item__c invoiceItem : invoiceLineItemList)
                        {
                            if(tempInvoiceObject.Invoice_Type__c == 'Guarantee')
                            {
                                //set the Invoice Id...
                                if(invoiceItem.Fee_Type__c == 'Guarantee')
                                    invoiceItem.Invoice__c = tempInvoiceObject.Id;
                            }
                            else if(tempInvoiceObject.Invoice_Type__c == 'Non Refundable')
                            {
                                //set the Invoice Id...
                                if(invoiceItem.Fee_Type__c != 'Guarantee')
                                    invoiceItem.Invoice__c = tempInvoiceObject.Id;
                            }
                            
                            System.debug('The Invoice Item Fee Type => '+ invoiceItem.Fee_Type__c);
                            System.debug('The Invoice Item Invoice Value => '+ invoiceItem.Invoice__c);
                            
                        }   
                        
                    }
                        
                }
                                
                //Now Insert the Invoice Line Item...
                if(invoiceLineItemList.size()> 0 )
                    insert invoiceLineItemList;
                    
                for(Request__c serviceObject : [SELECT ID,Request_Status__c FROM Request__c where Id =: serviceLetterId])
                {
                    serviceObject.Request_Status__c = 'Invoice Issued';update serviceObject;
                }      
            }
            
            //Returning the Value...
            return resultList;    
            
        }
        catch(System.Exception exp)
        {
            System.debug('The Error is => '+ exp.getMessage());
            Database.rollback(sp);
            return null;
        
        }
        
    }
    //For Generating the Invoice Button For Applications Object...
    //WebService static List<string> generatingInvoiceAndLineItemForApplicationMethod(ID accountId,ID applicationId, string transactionType)
    WebService static List<string> generatingInvoiceAndLineItemForApplicationMethod(ID accountId,ID applicationId, string transactionType,string isPerformaInvoice)
    {
        Savepoint sp = Database.setSavepoint();
        
        //Query to the Service Price Book For Invoice and Invoice Line Item...
        List<Service_Price_Book__c> servicePriceBookList = new List<Service_Price_Book__c>([Select Employment_ID_Card__c,AFZA_Employment_ID_Card__c,Advertisement_Fee__c,AFZA_Advertisement_Fee__c,AFZA_Establishment_Card__c,AFZA_Etisalat__c,AFZA_General_Services__c,AFZA_Google__c,AFZA_License_Activity_Fee__c,License_Activity_Fee__c,AFZA_Maintenance__c,AFZA_Sign_Board__c,AFZA_Sewage__c,General_Services__c, Establishment_Card__c, Etisalat__c, Google__c, Gov_Establishment_Card__c, Maintenance__c, Sewage__c, Sign_Board__c,Name,AFZA_Charges__c, AFZA_Other_Charges__c,  EMPost_Charges__c, Local_Government_Charges__c, License_Application_Type__c,Fee_Type__c,License_App_Category__c, Id, Fee__r.Fee_ID__c,Visa_Category__c, Fee__r.Fee_Type__c, Fee__r.Receipt_Group__c, Fee__r.Price__c, Fee__r.Name, Fee__c, Description__c From Service_Price_Book__c where  License_Application_Type__c =: 'Application' AND Visa_Category__c =: transactionType AND isActive__c =: true]);
        List<Fines__c> suspensionInvoiceItemList = new List<Fines__c>([Select Id,Account__c,Status__c, Name,Fines_Included__c, Fine_Total_Amount_Due__c From Fines__c where Fines_Included__c = true and Status__c = 'Approved' AND Account__c =: accountId]); 
        
        //Variables...
        Map<string,Invoice__c> invoiceObjectMap = new Map<string,Invoice__c>();
        Invoice_Line_Item__c tempInvoiceItem = null;
        List<Invoice_Line_Item__c> invoiceLineItemList = new List<Invoice_Line_Item__c>();
        list<String> resultList = new list<string>();
        
        try
        {
            //Create a Invoice Object.... For Non Refundable...
            Invoice__c tempInvoice = new Invoice__c();
            tempInvoice.Account__c = accountId;
            tempInvoice.Application__c = applicationId;
            tempInvoice.Date__c = System.Today();
            tempInvoice.Invoice_Type__c = 'Non Refundable';
            tempInvoice.Fee_Application_Type__c = 'Application';
            
            if(isPerformaInvoice == 'True')
                tempInvoice.isPerformaInvoice__c = true;
            
            //Adding in Map....
            invoiceObjectMap.put('Non Refundable',tempInvoice); 
            
            //Create a Invoice Object.... For Guarantee...
            Invoice__c tempInvoice1 = new Invoice__c();
            tempInvoice1.Account__c = accountId;
            tempInvoice1.Application__c = applicationId;
            tempInvoice1.Date__c = System.Today();
            tempInvoice1.Invoice_Type__c = 'Guarantee';
            tempInvoice1.Fee_Application_Type__c = 'Application';
            
            if(isPerformaInvoice == 'True')
                tempInvoice.isPerformaInvoice__c = true;
                
            //Adding in Map....
            invoiceObjectMap.put('Guarantee',tempInvoice1); 
            
            //For Guarantee Flag...
            Boolean guaranteeInvoiceFlag = false;
            
            
            //From Suspension Invoice Items....
            if(suspensionInvoiceItemList.size() > 0)
            {
                for(Fines__c suspensionObject : suspensionInvoiceItemList)
                {
                    //We only neeed to put the Invoice Line Item...
                    tempInvoiceItem = new Invoice_Line_Item__c();
                    
                    //Mapping the Fields...
                    tempInvoiceItem.Name = suspensionObject.Name;
                    tempInvoiceItem.Amount__c = suspensionObject.Fine_Total_Amount_Due__c;
                    tempInvoiceItem.Suspension__c = suspensionObject.Id;
                    
                    //Now Add the Invoice Item...
                    invoiceLineItemList.add(tempInvoiceItem);
                    
                }
            }
            
                    
            //For Service Price Book....
            If(servicePriceBookList.size() > 0)
            {
                for(Service_Price_Book__c servicePriceBookObject : servicePriceBookList )
                {
                    if(servicePriceBookObject.Fee__r.Fee_Type__c == 'Guarantee')
                        guaranteeInvoiceFlag = true;
                    
                    //We only neeed to put the Invoice Line Item...
                    tempInvoiceItem = new Invoice_Line_Item__c();
                    
                    //Mapping the Fields...
                    tempInvoiceItem.Name = servicePriceBookObject.Fee__r.Name;
                    tempInvoiceItem.Fee__c = servicePriceBookObject.Fee__r.Id;
                    tempInvoiceItem.Fee_Type__c = servicePriceBookObject.Fee__r.Fee_Type__c;
                    tempInvoiceItem.Amount__c = servicePriceBookObject.Fee__r.Price__c;
                    tempInvoiceItem.Service_Price_Book__c = servicePriceBookObject.Id;
                    
                    tempInvoiceItem.AFZA_Charges__c = servicePriceBookObject.AFZA_Charges__c;
                    tempInvoiceItem.AFZA_Other_Charges__c = servicePriceBookObject.AFZA_Other_Charges__c;
                    tempInvoiceItem.EMPost_Charges__c = servicePriceBookObject.EMPost_Charges__c;
                    tempInvoiceItem.Local_Government_Charges__c = servicePriceBookObject.Local_Government_Charges__c;
                    
                    tempInvoiceItem.Maintenance__c = servicePriceBookObject.Maintenance__c;
                    tempInvoiceItem.Establishment_Card__c = servicePriceBookObject.Establishment_Card__c;
                    tempInvoiceItem.Etisalat__c = servicePriceBookObject.Etisalat__c;
                    tempInvoiceItem.Google__c = servicePriceBookObject.Google__c;
                    tempInvoiceItem.Gov_Establishment_Card__c = servicePriceBookObject.Gov_Establishment_Card__c;
                    tempInvoiceItem.Sewage__c = servicePriceBookObject.Sewage__c;
                    tempInvoiceItem.Sign_Board__c = servicePriceBookObject.Sign_Board__c;
                    tempInvoiceItem.General_Services__c = servicePriceBookObject.General_Services__c;
                    
                    tempInvoiceItem.AFZA_Maintenance__c = servicePriceBookObject.AFZA_Maintenance__c;
                    tempInvoiceItem.AFZA_Establishment_Card__c = servicePriceBookObject.AFZA_Establishment_Card__c;
                    tempInvoiceItem.AFZA_Etisalat__c = servicePriceBookObject.AFZA_Etisalat__c;
                    tempInvoiceItem.AFZA_Google__c = servicePriceBookObject.AFZA_Google__c;
                    tempInvoiceItem.AFZA_Sewage__c = servicePriceBookObject.AFZA_Sewage__c;
                    tempInvoiceItem.AFZA_Sign_Board__c = servicePriceBookObject.AFZA_Sign_Board__c;
                    tempInvoiceItem.AFZA_General_Services__c = servicePriceBookObject.AFZA_General_Services__c;
                    
                    tempInvoiceItem.Advertisement_Fee__c = servicePriceBookObject.Advertisement_Fee__c;
                    tempInvoiceItem.AFZA_Advertisement_Fee__c = servicePriceBookObject.AFZA_Advertisement_Fee__c;
                    tempInvoiceItem.License_Activity_Fee__c = servicePriceBookObject.License_Activity_Fee__c;
                    tempInvoiceItem.AFZA_License_Activity_Fee__c = servicePriceBookObject.AFZA_License_Activity_Fee__c;
                    
                    tempInvoiceItem.Employment_ID_Card__c = servicePriceBookObject.Employment_ID_Card__c;
                    tempInvoiceItem.AFZA_Employment_ID_Card__c = servicePriceBookObject.AFZA_Employment_ID_Card__c;
                    
                    
                    
                    //Now Add the Invoice Item...
                    invoiceLineItemList.add(tempInvoiceItem);
                    
                    
                }
            
            }

            //Now create the Invoice records...
            if(invoiceObjectMap.size() > 0)
            {
                
                //For Guarantee Invoice Remove if its not in placed...
                if(guaranteeInvoiceFlag != true)
                    invoiceObjectMap.remove('Guarantee');
                System.debug('The Invoice Object Map Size => '+ invoiceObjectMap.size());
                //First Insert the invoice...
                insert invoiceObjectMap.values();
                
                for(Invoice__c tempInvoiceObject : invoiceObjectMap.values())
                {
                    //Putting in the Result List...
                    resultList.add(tempInvoiceObject.Id);
                    
                    //Set the Invoice Id in the Invoice Line Item...
                    if(invoiceLineItemList.size()> 0)
                    {
                        for(Invoice_Line_Item__c invoiceItem : invoiceLineItemList)
                        {
                            if(tempInvoiceObject.Invoice_Type__c == 'Guarantee')
                            {
                                //set the Invoice Id...
                                if(invoiceItem.Fee_Type__c == 'Guarantee')
                                    invoiceItem.Invoice__c = tempInvoiceObject.Id;
                            }
                            else if(tempInvoiceObject.Invoice_Type__c == 'Non Refundable')
                            {
                                //set the Invoice Id...
                                if(invoiceItem.Fee_Type__c != 'Guarantee')
                                    invoiceItem.Invoice__c = tempInvoiceObject.Id;
                            }
                            
                            System.debug('The Invoice Item Fee Type => '+ invoiceItem.Fee_Type__c);
                            System.debug('The Invoice Item Invoice Value => '+ invoiceItem.Invoice__c);
                            
                        }   
                        
                    }
                        
                }
                                
                //Now Insert the Invoice Line Item...
                if(invoiceLineItemList.size()> 0 )
                    insert invoiceLineItemList;
              
            }
            
            //Returning the Value...
            return resultList;    
            
        }
        catch(System.Exception exp)
        {
            System.debug('The Error is => '+ exp.getMessage());
            Database.rollback(sp);
            return null;
        
        }
        
    }
    
    //For Generating the Invoice Button For Tracking Object...
    //WebService static List<string> generatingInvoiceAndLineItemForTrackingMethod(ID accountId,ID trackingId, string transactionType)
    WebService static List<string> generatingInvoiceAndLineItemForTrackingMethod(ID accountId,ID trackingId, string transactionType,string isPerformaInvoice)
    {
        Savepoint sp = Database.setSavepoint();
        
        //Query to the Service Price Book For Invoice and Invoice Line Item...
        List<Service_Price_Book__c> servicePriceBookList = new List<Service_Price_Book__c>([Select Employment_ID_Card__c,AFZA_Employment_ID_Card__c,Advertisement_Fee__c,AFZA_Advertisement_Fee__c,AFZA_Establishment_Card__c,AFZA_Etisalat__c,AFZA_General_Services__c,AFZA_Google__c,AFZA_License_Activity_Fee__c,License_Activity_Fee__c,AFZA_Maintenance__c,AFZA_Sign_Board__c,AFZA_Sewage__c,General_Services__c, Establishment_Card__c, Etisalat__c, Google__c, Gov_Establishment_Card__c, Maintenance__c, Sewage__c, Sign_Board__c,Name, AFZA_Charges__c, AFZA_Other_Charges__c,EMPost_Charges__c, Local_Government_Charges__c,License_Application_Type__c,Fee_Type__c,License_App_Category__c, Id, Fee__r.Fee_ID__c,Visa_Category__c, Fee__r.Fee_Type__c, Fee__r.Receipt_Group__c, Fee__r.Price__c, Fee__r.Name, Fee__c, Description__c From Service_Price_Book__c where  License_Application_Type__c =: 'Tracking' AND Visa_Category__c =: transactionType AND isActive__c =: true]);
        List<Fines__c> suspensionInvoiceItemList = new List<Fines__c>([Select Id,Account__c,Status__c, Name,Fines_Included__c, Fine_Total_Amount_Due__c From Fines__c where Fines_Included__c = true and Status__c = 'Approved' AND Account__c =: accountId]); 
        
        //Variables...
        Map<string,Invoice__c> invoiceObjectMap = new Map<string,Invoice__c>();
        Invoice_Line_Item__c tempInvoiceItem = null;
        List<Invoice_Line_Item__c> invoiceLineItemList = new List<Invoice_Line_Item__c>();
        list<String> resultList = new list<string>();
        
        try
        {
            //Create a Invoice Object.... For Non Refundable...
            Invoice__c tempInvoice = new Invoice__c();
            tempInvoice.Account__c = accountId;
            tempInvoice.Tracking__c = trackingId;
            tempInvoice.Date__c = System.Today();
            tempInvoice.Invoice_Type__c = 'Non Refundable';
            tempInvoice.Fee_Application_Type__c = 'Tracking';
            
            if(isPerformaInvoice == 'True')
                tempInvoice.isPerformaInvoice__c = true;
            //Adding in Map....
            invoiceObjectMap.put('Non Refundable',tempInvoice); 
            
            //Create a Invoice Object.... For Guarantee...
            Invoice__c tempInvoice1 = new Invoice__c();
            tempInvoice1.Account__c = accountId;
            tempInvoice1.Tracking__c = trackingId;
            tempInvoice1.Date__c = System.Today();
            tempInvoice1.Invoice_Type__c = 'Guarantee';
            tempInvoice1.Fee_Application_Type__c = 'Tracking';
            
            if(isPerformaInvoice == 'True')
                tempInvoice1.isPerformaInvoice__c = true;
            
            //Adding in Map....
            invoiceObjectMap.put('Guarantee',tempInvoice1); 
            
            //For Guarantee Flag...
            Boolean guaranteeInvoiceFlag = false;
            
            //From Suspension Invoice Items....
            if(suspensionInvoiceItemList.size() > 0)
            {
                for(Fines__c suspensionObject : suspensionInvoiceItemList)
                {
                    //We only neeed to put the Invoice Line Item...
                    tempInvoiceItem = new Invoice_Line_Item__c();
                    
                    //Mapping the Fields...
                    tempInvoiceItem.Name = suspensionObject.Name;
                    tempInvoiceItem.Amount__c = suspensionObject.Fine_Total_Amount_Due__c;
                    tempInvoiceItem.Suspension__c = suspensionObject.Id;
                    
                    //Now Add the Invoice Item...
                    invoiceLineItemList.add(tempInvoiceItem);
                    
                }
            }
                        
            //For Service Price Book....
            If(servicePriceBookList.size() > 0)
            {
                for(Service_Price_Book__c servicePriceBookObject : servicePriceBookList )
                {
                    if(servicePriceBookObject.Fee__r.Fee_Type__c == 'Guarantee')
                        guaranteeInvoiceFlag = true;
                    
                    //We only neeed to put the Invoice Line Item...
                    tempInvoiceItem = new Invoice_Line_Item__c();
                    
                    //Mapping the Fields...
                    tempInvoiceItem.Name = servicePriceBookObject.Fee__r.Name;
                    tempInvoiceItem.Fee__c = servicePriceBookObject.Fee__r.Id;
                    tempInvoiceItem.Fee_Type__c = servicePriceBookObject.Fee__r.Fee_Type__c;
                    tempInvoiceItem.Amount__c = servicePriceBookObject.Fee__r.Price__c;
                    tempInvoiceItem.Service_Price_Book__c = servicePriceBookObject.Id;
                    
                    tempInvoiceItem.AFZA_Charges__c = servicePriceBookObject.AFZA_Charges__c;
                    tempInvoiceItem.AFZA_Other_Charges__c = servicePriceBookObject.AFZA_Other_Charges__c;
                    tempInvoiceItem.EMPost_Charges__c = servicePriceBookObject.EMPost_Charges__c;
                    tempInvoiceItem.Local_Government_Charges__c = servicePriceBookObject.Local_Government_Charges__c;
                    
                    tempInvoiceItem.Maintenance__c = servicePriceBookObject.Maintenance__c;
                    tempInvoiceItem.Establishment_Card__c = servicePriceBookObject.Establishment_Card__c;
                    tempInvoiceItem.Etisalat__c = servicePriceBookObject.Etisalat__c;
                    tempInvoiceItem.Google__c = servicePriceBookObject.Google__c;
                    tempInvoiceItem.Gov_Establishment_Card__c = servicePriceBookObject.Gov_Establishment_Card__c;
                    tempInvoiceItem.Sewage__c = servicePriceBookObject.Sewage__c;
                    tempInvoiceItem.Sign_Board__c = servicePriceBookObject.Sign_Board__c;
                    tempInvoiceItem.General_Services__c = servicePriceBookObject.General_Services__c;
                    
                    tempInvoiceItem.AFZA_Maintenance__c = servicePriceBookObject.AFZA_Maintenance__c;
                    tempInvoiceItem.AFZA_Establishment_Card__c = servicePriceBookObject.AFZA_Establishment_Card__c;
                    tempInvoiceItem.AFZA_Etisalat__c = servicePriceBookObject.AFZA_Etisalat__c;
                    tempInvoiceItem.AFZA_Google__c = servicePriceBookObject.AFZA_Google__c;
                    tempInvoiceItem.AFZA_Sewage__c = servicePriceBookObject.AFZA_Sewage__c;
                    tempInvoiceItem.AFZA_Sign_Board__c = servicePriceBookObject.AFZA_Sign_Board__c;
                    tempInvoiceItem.AFZA_General_Services__c = servicePriceBookObject.AFZA_General_Services__c;
                                        
                    tempInvoiceItem.Advertisement_Fee__c = servicePriceBookObject.Advertisement_Fee__c;
                    tempInvoiceItem.AFZA_Advertisement_Fee__c = servicePriceBookObject.AFZA_Advertisement_Fee__c;
                    tempInvoiceItem.License_Activity_Fee__c = servicePriceBookObject.License_Activity_Fee__c;
                    tempInvoiceItem.AFZA_License_Activity_Fee__c = servicePriceBookObject.AFZA_License_Activity_Fee__c;
                    
                    tempInvoiceItem.Employment_ID_Card__c = servicePriceBookObject.Employment_ID_Card__c;
                    tempInvoiceItem.AFZA_Employment_ID_Card__c = servicePriceBookObject.AFZA_Employment_ID_Card__c;
                    
                    //Now Add the Invoice Item...
                    invoiceLineItemList.add(tempInvoiceItem);
                    
                }
            
            }

            //Now create the Invoice records...
            if(invoiceObjectMap.size() > 0)
            {                
                //For Guarantee Invoice Remove if its not in placed...
                if(guaranteeInvoiceFlag != true)
                    invoiceObjectMap.remove('Guarantee');
                System.debug('The Invoice Object Map Size => '+ invoiceObjectMap.size());
                //First Insert the invoice...
                insert invoiceObjectMap.values();
                
                for(Invoice__c tempInvoiceObject : invoiceObjectMap.values())
                {
                    //Putting in the Result List...
                    resultList.add(tempInvoiceObject.Id);
                    
                    //Set the Invoice Id in the Invoice Line Item...
                    if(invoiceLineItemList.size()> 0)
                    {
                        for(Invoice_Line_Item__c invoiceItem : invoiceLineItemList)
                        {
                            if(tempInvoiceObject.Invoice_Type__c == 'Guarantee')
                            {
                                //set the Invoice Id...
                                if(invoiceItem.Fee_Type__c == 'Guarantee')
                                    invoiceItem.Invoice__c = tempInvoiceObject.Id;
                            }
                            else if(tempInvoiceObject.Invoice_Type__c == 'Non Refundable')
                            {
                                //set the Invoice Id...
                                if(invoiceItem.Fee_Type__c != 'Guarantee')
                                    invoiceItem.Invoice__c = tempInvoiceObject.Id;
                            }
                            
                            System.debug('The Invoice Item Fee Type => '+ invoiceItem.Fee_Type__c);
                            System.debug('The Invoice Item Invoice Value => '+ invoiceItem.Invoice__c);
                            
                        }   
                        
                    }
                        
                }
                                
                //Now Insert the Invoice Line Item...
                if(invoiceLineItemList.size()> 0 )
                    insert invoiceLineItemList;
              
            }
            
            //Returning the Value...
            return resultList;    
            
        }
        catch(System.Exception exp)
        {
            System.debug('The Error is => '+ exp.getMessage());
            Database.rollback(sp);
            return null;
        
        }
        
    }
     
    //For Generating the Invoice Button For New License New Approach (05/March/2012)....
    //WebService static List<string> generatingInvoiceAndLineItemForNewLicenseMethod(ID accountId, string licenseApplicationType, string ActivityType)
    //WebService static List<string> generatingInvoiceAndLineItemForNewLicenseMethod(ID accountId, string licenseApplicationType, string ActivityType,List<Payment_Plan__c> paymentPlanList)
    WebService static List<string> generatingInvoiceAndLineItemForNewLicenseMethod(ID accountId, string licenseApplicationType, string ActivityType,string isPerformaInvoice,List<Payment_Plan__c> paymentPlanList)
    {
        Savepoint sp = Database.setSavepoint();
        
        //Query to the Service Price Book For Invoice and Invoice Line Item...
        List<Service_Price_Book__c> servicePriceBookList = new List<Service_Price_Book__c>([Select Employment_ID_Card__c,AFZA_Employment_ID_Card__c,Advertisement_Fee__c,AFZA_Advertisement_Fee__c,AFZA_Establishment_Card__c,AFZA_Etisalat__c,AFZA_General_Services__c,AFZA_Google__c,AFZA_License_Activity_Fee__c,License_Activity_Fee__c,AFZA_Maintenance__c,AFZA_Sign_Board__c,AFZA_Sewage__c,General_Services__c, Establishment_Card__c, Etisalat__c, Google__c, Gov_Establishment_Card__c, Maintenance__c, Sewage__c, Sign_Board__c,Name,AFZA_Charges__c, AFZA_Other_Charges__c,  EMPost_Charges__c, Local_Government_Charges__c, License_Application_Type__c,Fee_Type__c,License_App_Category__c, Id, Fee__r.Fee_ID__c, Fee__r.Fee_Type__c, Fee__r.Receipt_Group__c, Fee__r.Price__c, Fee__r.Name, Fee__c, Description__c From Service_Price_Book__c where  License_Application_Type__c =: licenseApplicationType AND License_App_Category__c =: ActivityType AND Fee_Type__c = 'Non Refundable' AND isActive__c =: true]);
        List<Service_Price_Book__c> servicePriceBookLocationList = new List<Service_Price_Book__c>([Select Employment_ID_Card__c,AFZA_Employment_ID_Card__c,Advertisement_Fee__c,AFZA_Advertisement_Fee__c,AFZA_Establishment_Card__c,AFZA_Etisalat__c,AFZA_General_Services__c,AFZA_Google__c,AFZA_License_Activity_Fee__c,License_Activity_Fee__c,AFZA_Maintenance__c,AFZA_Sign_Board__c,AFZA_Sewage__c,General_Services__c, Establishment_Card__c, Etisalat__c, Google__c, Gov_Establishment_Card__c, Maintenance__c, Sewage__c, Sign_Board__c,Name,AFZA_Charges__c, AFZA_Other_Charges__c,  EMPost_Charges__c, Local_Government_Charges__c, Location_Type__c,FeeName__c,Price__c, License_Application_Type__c,Fee_Type__c,License_App_Category__c, Id, Fee__r.Fee_ID__c, Fee__r.Fee_Type__c, Fee__r.Receipt_Group__c, Fee__r.Price__c, Fee__r.Name, Fee__c, Description__c From Service_Price_Book__c where  License_Application_Type__c =: licenseApplicationType AND License_App_Category__c =: 'Location' AND isActive__c =: true]);
        List<Location_Booking_Information__c> locationBookingList = new List<Location_Booking_Information__c>([Select Location__r.Name,Location__r.Location_Type__c,Location__r.Rental_Rate__c,Location__r.Fee__r.Name, Location__r.Fee__r.Fee_Type__c, Location__r.Id, Location__c, License_Booking_Status__c, Id, Booking_Released_Date__c, Booking_Leasing_Issue_Date__c, Booking_Date__c, Account__c From Location_Booking_Information__c where Account__c =: accountId AND License_Booking_Status__c = 'Booked' ]);
        
           
        Map<string,Invoice__c> invoiceObjectMap = new Map<string,Invoice__c>();
        Invoice_Line_Item__c tempInvoiceItem = null;
        List<Invoice_Line_Item__c> invoiceLineItemList = new List<Invoice_Line_Item__c>();
        list<String> resultList = new list<string>();
        
        //For Packages...
        Map<string,Payment_Plan__c> paymentPlanMap = new Map<string,Payment_Plan__c>();
        if(paymentPlanList == null)
            paymentPlanList = new List<Payment_Plan__c>();
        
        
        
        try
        {
            //Create a Invoice Object.... For Non Refundable...
            Invoice__c tempInvoice = new Invoice__c();
            tempInvoice.Account__c = accountId;
            tempInvoice.Date__c = System.Today();
            //tempInvoice.Invoice_Type__c = servicePriceBookObject.Fee__r.Fee_Type__c;
            tempInvoice.Invoice_Type__c = 'Non Refundable';
            tempInvoice.Fee_Application_Type__c = 'New License';
            
            System.debug('In New License Method Peforma Invoice=>  '+ isPerformaInvoice);
            if(isPerformaInvoice == 'True')
                tempInvoice.isPerformaInvoice__c = true;    
            
            //Adding in Map....
            invoiceObjectMap.put('Non Refundable',tempInvoice); 
            
            //Create a Invoice Object.... For Guarantee...
            Invoice__c tempInvoice1 = new Invoice__c();
            tempInvoice1.Account__c = accountId;
            tempInvoice1.Date__c = System.Today();
            //tempInvoice1.Invoice_Type__c = servicePriceBookObject.Fee__r.Fee_Type__c;
            tempInvoice1.Invoice_Type__c = 'Guarantee';
            tempInvoice1.Fee_Application_Type__c = 'New License';
            
            if(isPerformaInvoice == 'True')
                tempInvoice1.isPerformaInvoice__c = true;
            
            //Adding in Map....
            invoiceObjectMap.put('Guarantee',tempInvoice1); 
            
            //For Guarantee Flag...
            Boolean guaranteeInvoiceFlag = false;
                        
            //For Service Price Book....
            If(servicePriceBookList.size() > 0)
            {
                for(Service_Price_Book__c servicePriceBookObject : servicePriceBookList )
                {
                    //We only neeed to put the Invoice Line Item...
                    tempInvoiceItem = new Invoice_Line_Item__c();
                    
                    //Mapping the Fields...
                    tempInvoiceItem.Name = servicePriceBookObject.Fee__r.Name;
                    tempInvoiceItem.Fee__c = servicePriceBookObject.Fee__r.Id;
                    tempInvoiceItem.Fee_Type__c = servicePriceBookObject.Fee__r.Fee_Type__c;
                    tempInvoiceItem.Amount__c = servicePriceBookObject.Fee__r.Price__c;
                    tempInvoiceItem.Service_Price_Book__c = servicePriceBookObject.Id;
                    
                    tempInvoiceItem.AFZA_Charges__c = servicePriceBookObject.AFZA_Charges__c;
                    tempInvoiceItem.AFZA_Other_Charges__c = servicePriceBookObject.AFZA_Other_Charges__c;
                    tempInvoiceItem.EMPost_Charges__c = servicePriceBookObject.EMPost_Charges__c;
                    tempInvoiceItem.Local_Government_Charges__c = servicePriceBookObject.Local_Government_Charges__c;
                    
                    tempInvoiceItem.Maintenance__c = servicePriceBookObject.Maintenance__c;
                    tempInvoiceItem.Establishment_Card__c = servicePriceBookObject.Establishment_Card__c;
                    tempInvoiceItem.Etisalat__c = servicePriceBookObject.Etisalat__c;
                    tempInvoiceItem.Google__c = servicePriceBookObject.Google__c;
                    tempInvoiceItem.Gov_Establishment_Card__c = servicePriceBookObject.Gov_Establishment_Card__c;
                    tempInvoiceItem.Sewage__c = servicePriceBookObject.Sewage__c;
                    tempInvoiceItem.Sign_Board__c = servicePriceBookObject.Sign_Board__c;
                    tempInvoiceItem.General_Services__c = servicePriceBookObject.General_Services__c;
                    
                    tempInvoiceItem.AFZA_Maintenance__c = servicePriceBookObject.AFZA_Maintenance__c;
                    tempInvoiceItem.AFZA_Establishment_Card__c = servicePriceBookObject.AFZA_Establishment_Card__c;
                    tempInvoiceItem.AFZA_Etisalat__c = servicePriceBookObject.AFZA_Etisalat__c;
                    tempInvoiceItem.AFZA_Google__c = servicePriceBookObject.AFZA_Google__c;
                    tempInvoiceItem.AFZA_Sewage__c = servicePriceBookObject.AFZA_Sewage__c;
                    tempInvoiceItem.AFZA_Sign_Board__c = servicePriceBookObject.AFZA_Sign_Board__c;
                    tempInvoiceItem.AFZA_General_Services__c = servicePriceBookObject.AFZA_General_Services__c;
                                        
                    tempInvoiceItem.Advertisement_Fee__c = servicePriceBookObject.Advertisement_Fee__c;
                    tempInvoiceItem.AFZA_Advertisement_Fee__c = servicePriceBookObject.AFZA_Advertisement_Fee__c;
                    tempInvoiceItem.License_Activity_Fee__c = servicePriceBookObject.License_Activity_Fee__c;
                    tempInvoiceItem.AFZA_License_Activity_Fee__c = servicePriceBookObject.AFZA_License_Activity_Fee__c;
                    
                    tempInvoiceItem.Employment_ID_Card__c = servicePriceBookObject.Employment_ID_Card__c;
                    tempInvoiceItem.AFZA_Employment_ID_Card__c = servicePriceBookObject.AFZA_Employment_ID_Card__c;
                                
                    //Now Add the Invoice Item...
                    invoiceLineItemList.add(tempInvoiceItem);
                    
                    
                }
            
            }
            
            //For Location Booking Invoice and Invoice Line Item..
            if(locationBookingList.size() > 0)
            {
                //For Package Selection Adding in the Map...
                if(paymentPlanList.size() > 0)
                {
                    for(Payment_Plan__c paymentPlanObject : paymentPlanList)
                    {
                        //Adding in the Map...
                        paymentPlanMap.put(paymentPlanObject.Location_Type__c,paymentPlanObject);
                    }
                }
                
                for(Location_Booking_Information__c bookingObject : locationBookingList)
                {
                    //Now Create a Invoice Line Item for this...
                    tempInvoiceItem = new Invoice_Line_Item__c();
                    //Mapping the Fields....
                    //tempInvoiceItem.Name = bookingObject.Location__r.Fee__r.Name;
                    //tempInvoiceItem.Name = bookingObject.Location__r.Name + ' Rental Rate';
                    tempInvoiceItem.Name = 'Location Rental Rate';
                    //tempInvoiceItem.Fee__c = bookingObject.Location__r.Fee__r.Id;
                    //tempInvoiceItem.Fee_Type__c = bookingObject.Location__r.Fee__r.Fee_Type__c;
                    tempInvoiceItem.Fee_Type__c = 'Non Refundable';
                    tempInvoiceItem.Amount__c = bookingObject.Location__r.Rental_Rate__c;
                    tempInvoiceItem.Location_Booking_Information__c = bookingObject.Id;
                    System.debug('Before The Simple Location Service Pricebook For Location ');
                    tempInvoiceItem.Location__c = bookingObject.Location__c;
                    System.debug('After The Simple Location Service Pricebook For Location ');
                    tempInvoiceItem.Start_Date__c = bookingObject.Booking_Date__c;
                    //tempInvoiceItem.End_Date__c = (bookingObject.Booking_Released_Date__c).addyears(1).adddays(-1);
                    tempInvoiceItem.End_Date__c = (bookingObject.Booking_Date__c).addyears(1).adddays(-1);
                    
                    
                    tempInvoiceItem.Location_Rental_Rate__c = bookingObject.Location__r.Rental_Rate__c;
                    tempInvoiceItem.AFZA_Location_Rental_Rate__c = bookingObject.Location__r.Rental_Rate__c;
                    
                    //Package Selection...
                    if(paymentPlanMap.get(bookingObject.Location__r.Location_Type__c) != null)
                    {
                        tempInvoiceItem.Name = 'Location Rental Rate ' + paymentPlanMap.get(bookingObject.Location__r.Location_Type__c).Installment__c + ' Installments';
                        tempInvoiceItem.Location_Rental_Rate__c = bookingObject.Location__r.Rental_Rate__c + (bookingObject.Location__r.Rental_Rate__c * paymentPlanMap.get(bookingObject.Location__r.Location_Type__c).Sign_Multiplier__c * (paymentPlanMap.get(bookingObject.Location__r.Location_Type__c).Percent__c / 100)    );
                        tempInvoiceItem.AFZA_Location_Rental_Rate__c = bookingObject.Location__r.Rental_Rate__c + (bookingObject.Location__r.Rental_Rate__c * paymentPlanMap.get(bookingObject.Location__r.Location_Type__c).Sign_Multiplier__c * (paymentPlanMap.get(bookingObject.Location__r.Location_Type__c).Percent__c / 100)    );
                        tempInvoiceItem.Payment_Plan__c = paymentPlanMap.get(bookingObject.Location__r.Location_Type__c).Id;
                        tempInvoiceItem.Package_Value__c = paymentPlanMap.get(bookingObject.Location__r.Location_Type__c).Sign_Multiplier__c * bookingObject.Location__r.Rental_Rate__c * (paymentPlanMap.get(bookingObject.Location__r.Location_Type__c).Percent__c / 100);
                        tempInvoiceItem.Installment__c = paymentPlanMap.get(bookingObject.Location__r.Location_Type__c).Installment__c;
                        tempInvoiceItem.Action__c = paymentPlanMap.get(bookingObject.Location__r.Location_Type__c).Action__c;
                    
                        //For Amount...
                        tempInvoiceItem.Amount__c = bookingObject.Location__r.Rental_Rate__c + (bookingObject.Location__r.Rental_Rate__c * paymentPlanMap.get(bookingObject.Location__r.Location_Type__c).Sign_Multiplier__c *(paymentPlanMap.get(bookingObject.Location__r.Location_Type__c).Percent__c / 100) );
                    }
                                
                    
                    //Now Add the Invoice Item in List...
                    invoiceLineItemList.add(tempInvoiceItem);
                    
                    //Also doing for the location... 
                    if(servicePriceBookLocationList.size() > 0)
                    {
                        //For the Service Price Book....
                        for(Service_Price_Book__c tempServiceObject : servicePriceBookLocationList)
                        {
                            //Office, Warehouse, Accomodation,Land, E.O.C, Shop...
                            if(tempServiceObject.Location_Type__c == bookingObject.Location__r.Location_Type__c)        
                            {
                                if(tempServiceObject.Fee_Type__c == 'Guarantee')
                                    guaranteeInvoiceFlag = true;
                                
                                //Now Create a Invoice Line Item for this...
                                tempInvoiceItem = new Invoice_Line_Item__c();
                                
                                //Mapping the Fields....
                                //tempInvoiceItem.Name = bookingObject.Location__r.Name + ' - '+ tempServiceObject.Location_Type__c +' Rental Rate';
                                //tempInvoiceItem.Name = bookingObject.Location__r.Name + ' - '+ tempServiceObject.FeeName__c +' Rental Rate';
                                //tempInvoiceItem.Name = tempServiceObject.FeeName__c +' Rental Rate';
                                tempInvoiceItem.Name = tempServiceObject.FeeName__c;
                                tempInvoiceItem.Fee__c = tempServiceObject.Fee__c;
                                tempInvoiceItem.Service_Price_Book__c = tempServiceObject.Id;
                                tempInvoiceItem.Fee_Type__c = tempServiceObject.Fee_Type__c;
                                tempInvoiceItem.Amount__c = tempServiceObject.Price__c;
                                tempInvoiceItem.Location_Booking_Information__c = bookingObject.Id;
                                System.debug('Before The Location Service Pricebook For Location ');
                                tempInvoiceItem.Location__c = bookingObject.Location__c;
                                System.debug('After The Location Service Pricebook For Location ');
                                //tempInvoiceItem.Start_Date__c = bookingObject.Booking_Date__c;
                                //tempInvoiceItem.End_Date__c = (bookingObject.Booking_Released_Date__c).addyears(1).adddays(-1);
                                tempInvoiceItem.AFZA_Charges__c = tempServiceObject.AFZA_Charges__c;
                                tempInvoiceItem.AFZA_Other_Charges__c = tempServiceObject.AFZA_Other_Charges__c;
                                tempInvoiceItem.EMPost_Charges__c = tempServiceObject.EMPost_Charges__c;
                                tempInvoiceItem.Local_Government_Charges__c = tempServiceObject.Local_Government_Charges__c;
                                
                                tempInvoiceItem.Maintenance__c = tempServiceObject.Maintenance__c;
                                tempInvoiceItem.Establishment_Card__c = tempServiceObject.Establishment_Card__c;
                                tempInvoiceItem.Etisalat__c = tempServiceObject.Etisalat__c;
                                tempInvoiceItem.Google__c = tempServiceObject.Google__c;
                                tempInvoiceItem.Gov_Establishment_Card__c = tempServiceObject.Gov_Establishment_Card__c;
                                tempInvoiceItem.Sewage__c = tempServiceObject.Sewage__c;
                                tempInvoiceItem.Sign_Board__c = tempServiceObject.Sign_Board__c;
                                tempInvoiceItem.General_Services__c = tempServiceObject.General_Services__c;
                                
                                tempInvoiceItem.AFZA_Maintenance__c = tempServiceObject.AFZA_Maintenance__c;
                                tempInvoiceItem.AFZA_Establishment_Card__c = tempServiceObject.AFZA_Establishment_Card__c;
                                tempInvoiceItem.AFZA_Etisalat__c = tempServiceObject.AFZA_Etisalat__c;
                                tempInvoiceItem.AFZA_Google__c = tempServiceObject.AFZA_Google__c;
                                tempInvoiceItem.AFZA_Sewage__c = tempServiceObject.AFZA_Sewage__c;
                                tempInvoiceItem.AFZA_Sign_Board__c = tempServiceObject.AFZA_Sign_Board__c;
                                tempInvoiceItem.AFZA_General_Services__c = tempServiceObject.AFZA_General_Services__c;
                                                    
                                tempInvoiceItem.Advertisement_Fee__c = tempServiceObject.Advertisement_Fee__c;
                                tempInvoiceItem.AFZA_Advertisement_Fee__c = tempServiceObject.AFZA_Advertisement_Fee__c;
                                tempInvoiceItem.License_Activity_Fee__c = tempServiceObject.License_Activity_Fee__c;
                                tempInvoiceItem.AFZA_License_Activity_Fee__c = tempServiceObject.AFZA_License_Activity_Fee__c;
                                
                                tempInvoiceItem.Employment_ID_Card__c = tempServiceObject.Employment_ID_Card__c;
                                tempInvoiceItem.AFZA_Employment_ID_Card__c = tempServiceObject.AFZA_Employment_ID_Card__c;
                                
                                
                                //Now Add the Invoice Item in List...
                                invoiceLineItemList.add(tempInvoiceItem);
                                
                            }
                        }
                    }
                   
                   
                   
                }
                
            }
            
            //Now create the Invoice records...
            if(invoiceObjectMap.size() > 0)
            {
                
                //For Guarantee Invoice Remove if its not in placed...
                if(guaranteeInvoiceFlag != true)
                    invoiceObjectMap.remove('Guarantee');
                System.debug('The Invoice Object Map Size => '+ invoiceObjectMap.size());
                //First Insert the invoice...
                insert invoiceObjectMap.values();
                
                for(Invoice__c tempInvoiceObject : invoiceObjectMap.values())
                {
                    //Putting in the Result List...
                    resultList.add(tempInvoiceObject.Id);
                    
                    //Set the Invoice Id in the Invoice Line Item...
                    if(invoiceLineItemList.size()> 0)
                    {
                        for(Invoice_Line_Item__c invoiceItem : invoiceLineItemList)
                        {
                            if(tempInvoiceObject.Invoice_Type__c == 'Guarantee')
                            {
                                //set the Invoice Id...
                                if(invoiceItem.Fee_Type__c == 'Guarantee')
                                    invoiceItem.Invoice__c = tempInvoiceObject.Id;
                            }
                            else if(tempInvoiceObject.Invoice_Type__c == 'Non Refundable')
                            {
                                //set the Invoice Id...
                                if(invoiceItem.Fee_Type__c != 'Guarantee')
                                    invoiceItem.Invoice__c = tempInvoiceObject.Id;
                            }
                            
                            System.debug('The Invoice Item Fee Type => '+ invoiceItem.Fee_Type__c);
                            System.debug('The Invoice Item Invoice Value => '+ invoiceItem.Invoice__c);
                            
                        }   
                        
                    }
                        
                }
                
                
                //Now Insert the Invoice Line Item...
                if(invoiceLineItemList.size()> 0 )
                    insert invoiceLineItemList;
              
                if(isPerformaInvoice != 'True')
                {
                    //Change the status of Account to 'Invoice Issued'...
                    Account accountObject = new Account();
                    accountObject = [select Id, License_Application_Step__c from Account where Id =: accountId];
                    if(accountObject != null)
                    {
                        accountObject.License_Application_Step__c = 'Invoice Issued';
                        update accountObject;
                    }
                }
                
            }
            
            //Returning the Value...
            return resultList;    
            
        }
        catch(System.Exception exp)
        {
            System.debug('New License The Error Line Number => '+ exp.getLineNumber() +'The Error is => '+ exp.getMessage() );
            Database.rollback(sp);
            return null;
        
        }
       
    }
    
    //For Generating the Invoice Button for Amendment Object Process....
    //Webservice static List<String> generatingAmendmentInvoiceAndLineItemForAmendmentMethod(ID accountId,ID amendmentId,string recordTypeName,string accountlegalStatus)
    //Webservice static List<String> generatingAmendmentInvoiceAndLineItemForAmendmentMethod(ID accountId,ID amendmentId,string recordTypeName,string accountlegalStatus,List<Payment_Plan__c> paymentPlanList)
    Webservice static List<String> generatingAmendmentInvoiceAndLineItemForAmendmentMethod(ID accountId,ID amendmentId,string recordTypeName,string accountlegalStatus,string isPerformaInvoice,List<Payment_Plan__c> paymentPlanList)
    {
        Savepoint sp = Database.setSavepoint();
        list<String> resultList = new list<string>();
        List<Fines__c> suspensionInvoiceItemList = new List<Fines__c>([Select Id,Account__c,Status__c, Name,Fines_Included__c, Fine_Total_Amount_Due__c From Fines__c where Fines_Included__c = true and Status__c = 'Approved' AND Account__c =: accountId]);
        Map<string,Invoice__c> invoiceObjectMap = new Map<string,Invoice__c>();
        Invoice_Line_Item__c tempInvoiceItem = null;
        List<Invoice_Line_Item__c> invoiceLineItemList = new List<Invoice_Line_Item__c>();
        
        //Now Update the Amendment Object...
        Amendment__c amendmentObject = new Amendment__c();
        amendmentObject = [select Account__r.License_Expiry_Date__c,New_Location_Type_1__c,New_Location_Type_2__c,New_Location_Type_3__c,Account_License__c,New_License_Type__c,Total_Share_Holders__c,NTCDI__c,OTCDL__c,Company_Capital__c,New_Location_1_Name__c,New_Location_2_Name__c,New_Location_3_Name__c,New_Location__c,New_Location_2__c,New_Location_3__c,NLPPD_3__c,NLPPD_2__c,NLPPD__c,PTCPPD__c,Id,Keys_Required__c,Amendment_Application_Status__c from Amendment__c where Id =:amendmentId];
        
        //For Packages...
        Map<string,Payment_Plan__c> paymentPlanMap = new Map<string,Payment_Plan__c>();
        if(paymentPlanList == null)
            paymentPlanList = new List<Payment_Plan__c>();
            
        try
        {
            System.debug('The Account Id => '+ accountId);
            System.debug('The Amendment Object Id => '+ amendmentObject.id);
            
            //Create a Invoice Object.... Non Refundable...
            Invoice__c tempInvoiceObject = new Invoice__c();
            tempInvoiceObject.Account__c = accountId;
            tempInvoiceObject.Date__c = System.Today();
            tempInvoiceObject.Invoice_Type__c = 'Non Refundable';
            tempInvoiceObject.Fee_Application_Type__c = recordTypeName;
            tempInvoiceObject.Amendment__c = amendmentId;
            
            if(isPerformaInvoice == 'True')
                tempInvoiceObject.isPerformaInvoice__c = true;
           
            //Adding in Map....
            invoiceObjectMap.put('Non Refundable',tempInvoiceObject);
            
            //Create a Invoice Object.... Guarantee....
            Invoice__c tempInvoiceObject1 = new Invoice__c();
            tempInvoiceObject1.Account__c = accountId;
            tempInvoiceObject1.Date__c = System.Today();
            tempInvoiceObject1.Invoice_Type__c = 'Guarantee';
            tempInvoiceObject1.Fee_Application_Type__c = recordTypeName;
            tempInvoiceObject1.Amendment__c = amendmentId;
            
            if(isPerformaInvoice == 'True')
                tempInvoiceObject1.isPerformaInvoice__c = true;
                
            //Adding in Map....
            invoiceObjectMap.put('Guarantee',tempInvoiceObject1);

            //For Guarantee Invoice Item Flag...
            Boolean guaranteeInvoiceFlag = false;
                        
            //From Suspension Invoice Items....
            if(suspensionInvoiceItemList.size() > 0)
            {
                for(Fines__c suspensionObject : suspensionInvoiceItemList)
                {
                    //We only neeed to put the Invoice Line Item...
                    tempInvoiceItem = new Invoice_Line_Item__c();
                    
                    //Mapping the Fields...
                    tempInvoiceItem.Name = suspensionObject.Name;
                    tempInvoiceItem.Amount__c = suspensionObject.Fine_Total_Amount_Due__c;
                    tempInvoiceItem.Suspension__c = suspensionObject.Id;
                    
                    //Now Add the Invoice Item...
                    invoiceLineItemList.add(tempInvoiceItem);
                    
                }
            }
            
            if(recordTypeName == 'Location Amendment - Add Location')
            {
                //Change Location Flat Fee...
                /*Fee__c feeObject = new Fee__c();
                feeObject = [Select Id,Name,Price__c,Fee_Type__c,CreatedDate from Fee__c where  Fee_Type__c=: 'Amendment - Add Location' order by CreatedDate limit 1];*/
                
                List<Service_Price_Book__c> feeObject = new List<Service_Price_Book__c>([Select locationServiceFlag__c, Employment_ID_Card__c,AFZA_Employment_ID_Card__c,Advertisement_Fee__c,AFZA_Advertisement_Fee__c,AFZA_Establishment_Card__c,AFZA_Etisalat__c,AFZA_General_Services__c,AFZA_Google__c,AFZA_License_Activity_Fee__c,License_Activity_Fee__c,AFZA_Maintenance__c,AFZA_Sign_Board__c,AFZA_Sewage__c,General_Services__c, Establishment_Card__c, Etisalat__c, Google__c, Gov_Establishment_Card__c, Maintenance__c, Sewage__c, Sign_Board__c,AFZA_Charges__c, AFZA_Other_Charges__c,  EMPost_Charges__c, Local_Government_Charges__c,FeeName__c,Price__c, License_Application_Type__c, License_App_Category__c, Id, Fee__c, Fee_Type__c From Service_Price_Book__c where License_Application_Type__c =: 'Amendment' AND License_App_Category__c =: amendmentObject.Account_License__c AND Fee_Type__c=: 'Amendment - Add Location' AND isActive__c =: true order by CreatedDate limit 1]);
                //List<Service_Price_Book__c> servicePriceBookLocationList = new List<Service_Price_Book__c>([Select Advertisement_Fee__c,AFZA_Advertisement_Fee__c,AFZA_Establishment_Card__c,AFZA_Etisalat__c,AFZA_General_Services__c,AFZA_Google__c,AFZA_License_Activity_Fee__c,License_Activity_Fee__c,AFZA_Maintenance__c,AFZA_Sign_Board__c,AFZA_Sewage__c,General_Services__c, Establishment_Card__c, Etisalat__c, Google__c, Gov_Establishment_Card__c, Maintenance__c, Sewage__c, Sign_Board__c,Name, AFZA_Charges__c, AFZA_Other_Charges__c,EMPost_Charges__c, Local_Government_Charges__c,Location_Type__c,FeeName__c,Price__c, License_Application_Type__c,Fee_Type__c,License_App_Category__c, Id, Fee__r.Fee_ID__c, Fee__r.Fee_Type__c, Fee__r.Receipt_Group__c, Fee__r.Price__c, Fee__r.Name, Fee__c, Description__c From Service_Price_Book__c where  License_Application_Type__c =: 'New License' AND License_App_Category__c =: 'Location' AND isActive__c =: true]);             
                List<Service_Price_Book__c> servicePriceBookLocationList = new List<Service_Price_Book__c>([Select locationServiceFlag__c,Employment_ID_Card__c,AFZA_Employment_ID_Card__c,Advertisement_Fee__c,AFZA_Advertisement_Fee__c,AFZA_Establishment_Card__c,AFZA_Etisalat__c,AFZA_General_Services__c,AFZA_Google__c,AFZA_License_Activity_Fee__c,License_Activity_Fee__c,AFZA_Maintenance__c,AFZA_Sign_Board__c,AFZA_Sewage__c,General_Services__c, Establishment_Card__c, Etisalat__c, Google__c, Gov_Establishment_Card__c, Maintenance__c, Sewage__c, Sign_Board__c,Name, AFZA_Charges__c, AFZA_Other_Charges__c,EMPost_Charges__c, Local_Government_Charges__c,Location_Type__c,FeeName__c,Price__c, License_Application_Type__c,Fee_Type__c,License_App_Category__c, Id, Fee__r.Fee_ID__c, Fee__r.Fee_Type__c, Fee__r.Receipt_Group__c, Fee__r.Price__c, Fee__r.Name, Fee__c, Description__c From Service_Price_Book__c where  License_Application_Type__c =: 'Amendment' AND License_App_Category__c =: 'Location' AND isActive__c =: true]);
                
                if(feeObject.size() > 0)
                {
                    //We only neeed to put the Invoice Line Item...
                    tempInvoiceItem = new Invoice_Line_Item__c();
                    //Mapping the Fields...
                    //tempInvoiceItem.Name = feeObject[0].Name;
                    tempInvoiceItem.Name = feeObject[0].FeeName__c;
                    //tempInvoiceItem.Fee__c = feeObject[0].Id;
                    tempInvoiceItem.Fee__c = feeObject[0].Fee__c;
                    tempInvoiceItem.Service_Price_Book__c = feeObject[0].Id;
                    tempInvoiceItem.Fee_Type__c = feeObject[0].Fee_Type__c;
                    tempInvoiceItem.Amount__c = feeObject[0].Price__c;
                    
                    tempInvoiceItem.Amendment_Charges__c = feeObject[0].Price__c;
                    tempInvoiceItem.AFZA_Amendment_Charges__c = feeObject[0].Price__c;
                    
                    tempInvoiceItem.AFZA_Charges__c = feeObject[0].AFZA_Charges__c;
                    tempInvoiceItem.AFZA_Other_Charges__c = feeObject[0].AFZA_Other_Charges__c;
                    tempInvoiceItem.EMPost_Charges__c = feeObject[0].EMPost_Charges__c;
                    tempInvoiceItem.Local_Government_Charges__c = feeObject[0].Local_Government_Charges__c;
                    
                    tempInvoiceItem.Maintenance__c = feeObject[0].Maintenance__c;
                    tempInvoiceItem.Establishment_Card__c = feeObject[0].Establishment_Card__c;
                    tempInvoiceItem.Etisalat__c = feeObject[0].Etisalat__c;
                    tempInvoiceItem.Google__c = feeObject[0].Google__c;
                    tempInvoiceItem.Gov_Establishment_Card__c = feeObject[0].Gov_Establishment_Card__c;
                    tempInvoiceItem.Sewage__c = feeObject[0].Sewage__c;
                    tempInvoiceItem.Sign_Board__c = feeObject[0].Sign_Board__c;
                    tempInvoiceItem.General_Services__c = feeObject[0].General_Services__c;
                    
                    tempInvoiceItem.AFZA_Maintenance__c = feeObject[0].AFZA_Maintenance__c;
                    tempInvoiceItem.AFZA_Establishment_Card__c = feeObject[0].AFZA_Establishment_Card__c;
                    tempInvoiceItem.AFZA_Etisalat__c = feeObject[0].AFZA_Etisalat__c;
                    tempInvoiceItem.AFZA_Google__c = feeObject[0].AFZA_Google__c;
                    tempInvoiceItem.AFZA_Sewage__c = feeObject[0].AFZA_Sewage__c;
                    tempInvoiceItem.AFZA_Sign_Board__c = feeObject[0].AFZA_Sign_Board__c;
                    tempInvoiceItem.AFZA_General_Services__c = feeObject[0].AFZA_General_Services__c;
                    
                    tempInvoiceItem.Advertisement_Fee__c = feeObject[0].Advertisement_Fee__c;
                    tempInvoiceItem.AFZA_Advertisement_Fee__c = feeObject[0].AFZA_Advertisement_Fee__c;
                    tempInvoiceItem.License_Activity_Fee__c = feeObject[0].License_Activity_Fee__c;
                    tempInvoiceItem.AFZA_License_Activity_Fee__c = feeObject[0].AFZA_License_Activity_Fee__c;
                    
                    tempInvoiceItem.Employment_ID_Card__c = feeObject[0].Employment_ID_Card__c;
                    tempInvoiceItem.AFZA_Employment_ID_Card__c = feeObject[0].AFZA_Employment_ID_Card__c;
                    //Now Add the Invoice Item..
                    invoiceLineItemList.add(tempInvoiceItem);
                
                }
                
                //For Package Selection Adding in the Map...
                if(paymentPlanList.size() > 0)
                {
                    for(Payment_Plan__c paymentPlanObject : paymentPlanList)
                    {
                        //Adding in the Map...
                        paymentPlanMap.put(paymentPlanObject.Location_Type__c,paymentPlanObject);
                    }
                }
                        
                //For tenancy Cotract Invoice Item...
                if(amendmentObject.New_Location__c != null)
                {
                    
                    //We only neeed to put the Invoice Line Item...
                    tempInvoiceItem = new Invoice_Line_Item__c();
                    //Mapping the Fields...
                    //tempInvoiceItem.Name = 'Tenancy Contract Fee : '+ amendmentObject.New_Location_1_Name__c;
                    tempInvoiceItem.Name = 'Location Rental Rate';
                    tempInvoiceItem.Location__c = amendmentObject.New_Location__c;
                    System.debug('The Amendment Object New Location Invoice Item => '+ tempInvoiceItem.Name);
                    decimal tenancyPrice = ((amendmentObject.NLPPD__c * amendmentObject.NTCDI__c)).setScale(2);
                    
                    tempInvoiceItem.Start_Date__c = System.today();
                    
                    if(System.Today() < amendmentObject.Account__r.License_Expiry_Date__c)
                        tempInvoiceItem.End_Date__c = amendmentObject.Account__r.License_Expiry_Date__c;
                    else
                        tempInvoiceItem.End_Date__c = amendmentObject.Account__r.License_Expiry_Date__c.addYears(1);
                        
                        
                    if(tenancyPrice < 0)
                        tenancyPrice = 0;
                        
                    tempInvoiceItem.Amount__c = tenancyPrice;
                    tempInvoiceItem.Location_Rental_Rate__c = tenancyPrice;
                    tempInvoiceItem.AFZA_Location_Rental_Rate__c = tenancyPrice;
                    
                    //Package Selection...
                    if(paymentPlanMap.get(amendmentObject.New_Location_Type_1__c) != null)
                    {
                        tempInvoiceItem.Name = 'Location Rental Rate ' + paymentPlanMap.get(amendmentObject.New_Location_Type_1__c).Installment__c + ' Installments';
                        tempInvoiceItem.Location_Rental_Rate__c = tenancyPrice + (tenancyPrice * paymentPlanMap.get(amendmentObject.New_Location_Type_1__c).Sign_Multiplier__c * (paymentPlanMap.get(amendmentObject.New_Location_Type_1__c).Percent__c / 100)    );
                        tempInvoiceItem.AFZA_Location_Rental_Rate__c = tenancyPrice + (tenancyPrice * paymentPlanMap.get(amendmentObject.New_Location_Type_1__c).Sign_Multiplier__c * (paymentPlanMap.get(amendmentObject.New_Location_Type_1__c).Percent__c / 100)    );
                        tempInvoiceItem.Payment_Plan__c = paymentPlanMap.get(amendmentObject.New_Location_Type_1__c).Id;
                        tempInvoiceItem.Package_Value__c = tenancyPrice * paymentPlanMap.get(amendmentObject.New_Location_Type_1__c).Sign_Multiplier__c * (paymentPlanMap.get(amendmentObject.New_Location_Type_1__c).Percent__c / 100);
                        tempInvoiceItem.Installment__c = paymentPlanMap.get(amendmentObject.New_Location_Type_1__c).Installment__c;
                        tempInvoiceItem.Action__c = paymentPlanMap.get(amendmentObject.New_Location_Type_1__c).Action__c;
                        //The Amount...
                        tempInvoiceItem.Amount__c = (tenancyPrice + (tenancyPrice * paymentPlanMap.get(amendmentObject.New_Location_Type_1__c).Sign_Multiplier__c * (paymentPlanMap.get(amendmentObject.New_Location_Type_1__c).Percent__c / 100)    )).setScale(2);
                    }
                    
                    //Now Add the Invoice Item...
                    invoiceLineItemList.add(tempInvoiceItem);
                    
                    //Also doing for the location... 
                    if(servicePriceBookLocationList.size() > 0)
                    {
                        //For the Service Price Book....
                        for(Service_Price_Book__c tempServiceObject : servicePriceBookLocationList)
                        {
                            //Office, Warehouse, Accomodation,Land, E.O.C, Shop...
                            if(tempServiceObject.Location_Type__c == amendmentObject.New_Location_Type_1__c)        
                            {
                                
                                if(tempServiceObject.Fee_Type__c == 'Guarantee')
                                    guaranteeInvoiceFlag = true;
                                    
                                //Now Create a Invoice Line Item for this...
                                tempInvoiceItem = new Invoice_Line_Item__c();
                                //Mapping the Fields....
                                //tempInvoiceItem.Name = bookingObject.Location__r.Name + ' - '+ tempServiceObject.Location_Type__c +' Rental Rate';
                                //tempInvoiceItem.Name = amendmentObject.New_Location_1_Name__c + ' - '+ tempServiceObject.FeeName__c +' Rental Rate';
                                //tempInvoiceItem.Name = tempServiceObject.FeeName__c +' Rental Rate';
                                tempInvoiceItem.Name = tempServiceObject.FeeName__c;
                                tempInvoiceItem.Fee__c = tempServiceObject.Fee__c;
                                tempInvoiceItem.Location__c =  amendmentObject.New_Location__c;
                                tempInvoiceItem.Service_Price_Book__c = tempServiceObject.Id;
                                tempInvoiceItem.Fee_Type__c = tempServiceObject.Fee_Type__c;
                                
                                if(tempServiceObject.locationServiceFlag__c == 'False')
                                    tempInvoiceItem.Amount__c = tempServiceObject.Price__c;
                                else
                                {
                                    if(System.today() < amendmentObject.Account__r.License_Expiry_Date__c)
                                    {
                                        Decimal daybetweenValue = Decimal.valueof((System.today()).daysbetween(amendmentObject.Account__r.License_Expiry_Date__c)).setScale(2);
                                        tempInvoiceItem.Amount__c = ((tempServiceObject.Price__c / 360) * (daybetweenValue)).round(system.roundingMode.CEILING);
                                        
                                        tempInvoiceItem.Start_Date__c = System.today();
                                        tempInvoiceItem.End_Date__c = amendmentObject.Account__r.License_Expiry_Date__c;    
                                    }
                                    else
                                    {
                                        Decimal daybetweenValue = Decimal.valueof((System.today()).daysbetween(amendmentObject.Account__r.License_Expiry_Date__c.addyears(1))).setScale(2);
                                        tempInvoiceItem.Amount__c = ((tempServiceObject.Price__c / 360) * (daybetweenValue)).round(system.roundingMode.CEILING);
                                        
                                        tempInvoiceItem.Start_Date__c = System.today();
                                        tempInvoiceItem.End_Date__c = amendmentObject.Account__r.License_Expiry_Date__c.addYears(1);
                                    }
                                    
                                }   
                                
                                tempInvoiceItem.AFZA_Charges__c = tempServiceObject.AFZA_Charges__c;
                                tempInvoiceItem.AFZA_Other_Charges__c = tempServiceObject.AFZA_Other_Charges__c;
                                tempInvoiceItem.EMPost_Charges__c = tempServiceObject.EMPost_Charges__c;
                                tempInvoiceItem.Local_Government_Charges__c = tempServiceObject.Local_Government_Charges__c;
                                
                                tempInvoiceItem.Maintenance__c = tempServiceObject.Maintenance__c;
                                tempInvoiceItem.Establishment_Card__c = tempServiceObject.Establishment_Card__c;
                                tempInvoiceItem.Etisalat__c = tempServiceObject.Etisalat__c;
                                tempInvoiceItem.Google__c = tempServiceObject.Google__c;
                                tempInvoiceItem.Gov_Establishment_Card__c = tempServiceObject.Gov_Establishment_Card__c;
                                tempInvoiceItem.Sewage__c = tempServiceObject.Sewage__c;
                                tempInvoiceItem.Sign_Board__c = tempServiceObject.Sign_Board__c;
                                tempInvoiceItem.General_Services__c = tempServiceObject.General_Services__c;
                                
                                tempInvoiceItem.AFZA_Maintenance__c = tempServiceObject.AFZA_Maintenance__c;
                                tempInvoiceItem.AFZA_Establishment_Card__c = tempServiceObject.AFZA_Establishment_Card__c;
                                tempInvoiceItem.AFZA_Etisalat__c = tempServiceObject.AFZA_Etisalat__c;
                                tempInvoiceItem.AFZA_Google__c = tempServiceObject.AFZA_Google__c;
                                tempInvoiceItem.AFZA_Sewage__c = tempServiceObject.AFZA_Sewage__c;
                                tempInvoiceItem.AFZA_Sign_Board__c = tempServiceObject.AFZA_Sign_Board__c;
                                tempInvoiceItem.AFZA_General_Services__c = tempServiceObject.AFZA_General_Services__c;
                                
                                tempInvoiceItem.Advertisement_Fee__c = tempServiceObject.Advertisement_Fee__c;
                                tempInvoiceItem.AFZA_Advertisement_Fee__c = tempServiceObject.AFZA_Advertisement_Fee__c;
                                tempInvoiceItem.License_Activity_Fee__c = tempServiceObject.License_Activity_Fee__c;
                                tempInvoiceItem.AFZA_License_Activity_Fee__c = tempServiceObject.AFZA_License_Activity_Fee__c;
                                tempInvoiceItem.Employment_ID_Card__c = tempServiceObject.Employment_ID_Card__c;
                                tempInvoiceItem.AFZA_Employment_ID_Card__c = tempServiceObject.AFZA_Employment_ID_Card__c;                              
                                
                                
                                  
                                //Now Add the Invoice Item in List...
                                invoiceLineItemList.add(tempInvoiceItem);
                                
                            }
                        }
                    }
                    
                    
                    
                
                }
                
                if(amendmentObject.New_Location_2__c != null)
                {
                    //We only neeed to put the Invoice Line Item...
                    tempInvoiceItem = new Invoice_Line_Item__c();
                    //Mapping the Fields...
                    //tempInvoiceItem.Name = 'Tenancy Contract Fee : '+amendmentObject.New_Location_2_Name__c;
                    tempInvoiceItem.Name = 'Location Rental Rate';
                    tempInvoiceItem.Location__c = amendmentObject.New_Location_2__c;
                    System.debug('The Amendment Object New Location Invoice Item 2 => '+ tempInvoiceItem.Name);
                    decimal tenancyPrice = ((amendmentObject.NLPPD_2__c * amendmentObject.NTCDI__c)).setScale(2);
                    if(tenancyPrice < 0)
                        tenancyPrice = 0;
                    
                    
                    tempInvoiceItem.Start_Date__c = System.today();
                    
                    if(System.Today() < amendmentObject.Account__r.License_Expiry_Date__c)
                        tempInvoiceItem.End_Date__c = amendmentObject.Account__r.License_Expiry_Date__c;
                    else
                        tempInvoiceItem.End_Date__c = amendmentObject.Account__r.License_Expiry_Date__c.addYears(1);
                    
                        
                    tempInvoiceItem.Amount__c = tenancyPrice;
                    
                    tempInvoiceItem.Location_Rental_Rate__c = tenancyPrice;
                    tempInvoiceItem.AFZA_Location_Rental_Rate__c = tenancyPrice;
                    
                    //Package Selection...
                    if(paymentPlanMap.get(amendmentObject.New_Location_Type_2__c) != null)
                    {
                        tempInvoiceItem.Name = 'Location Rental Rate ' + paymentPlanMap.get(amendmentObject.New_Location_Type_2__c).Installment__c + ' Installments';
                        tempInvoiceItem.Location_Rental_Rate__c = tenancyPrice + (tenancyPrice * paymentPlanMap.get(amendmentObject.New_Location_Type_2__c).Sign_Multiplier__c * (paymentPlanMap.get(amendmentObject.New_Location_Type_2__c).Percent__c / 100)    );
                        tempInvoiceItem.AFZA_Location_Rental_Rate__c = tenancyPrice + (tenancyPrice * paymentPlanMap.get(amendmentObject.New_Location_Type_2__c).Sign_Multiplier__c * (paymentPlanMap.get(amendmentObject.New_Location_Type_2__c).Percent__c / 100)    );
                        tempInvoiceItem.Payment_Plan__c = paymentPlanMap.get(amendmentObject.New_Location_Type_2__c).Id;
                        tempInvoiceItem.Package_Value__c = tenancyPrice * paymentPlanMap.get(amendmentObject.New_Location_Type_2__c).Sign_Multiplier__c * (paymentPlanMap.get(amendmentObject.New_Location_Type_2__c).Percent__c / 100);
                        tempInvoiceItem.Installment__c = paymentPlanMap.get(amendmentObject.New_Location_Type_2__c).Installment__c;
                        tempInvoiceItem.Action__c = paymentPlanMap.get(amendmentObject.New_Location_Type_2__c).Action__c;
                        //The Amount...
                        tempInvoiceItem.Amount__c = (tenancyPrice + (tenancyPrice * paymentPlanMap.get(amendmentObject.New_Location_Type_2__c).Sign_Multiplier__c * (paymentPlanMap.get(amendmentObject.New_Location_Type_2__c).Percent__c / 100)    )).setScale(2);
                    }
                    
                    
                    //Now Add the Invoice Item...
                    invoiceLineItemList.add(tempInvoiceItem);
                    
                    //Also doing for the location... 
                    if(servicePriceBookLocationList.size() > 0)
                    {
                        //For the Service Price Book....
                        for(Service_Price_Book__c tempServiceObject : servicePriceBookLocationList)
                        {
                            //Office, Warehouse, Accomodation,Land, E.O.C, Shop...
                            if(tempServiceObject.Location_Type__c == amendmentObject.New_Location_Type_2__c)        
                            {
                                if(tempServiceObject.Fee_Type__c == 'Guarantee')
                                    guaranteeInvoiceFlag = true;
                                    
                                //Now Create a Invoice Line Item for this...
                                tempInvoiceItem = new Invoice_Line_Item__c();
                                //Mapping the Fields....
                                //tempInvoiceItem.Name = bookingObject.Location__r.Name + ' - '+ tempServiceObject.Location_Type__c +' Rental Rate';
                                //tempInvoiceItem.Name = amendmentObject.New_Location_2_Name__c + ' - '+ tempServiceObject.FeeName__c +' Rental Rate';
                                //tempInvoiceItem.Name = tempServiceObject.FeeName__c +' Rental Rate';
                                tempInvoiceItem.Name = tempServiceObject.FeeName__c;
                                tempInvoiceItem.Fee__c = tempServiceObject.Fee__c;
                                tempInvoiceItem.Location__c =  amendmentObject.New_Location_2__c;
                                tempInvoiceItem.Service_Price_Book__c = tempServiceObject.Id;
                                tempInvoiceItem.Fee_Type__c = tempServiceObject.Fee_Type__c;
                                //tempInvoiceItem.Amount__c = tempServiceObject.Price__c;
                                
                                if(tempServiceObject.locationServiceFlag__c == 'False')
                                    tempInvoiceItem.Amount__c = tempServiceObject.Price__c;
                                else
                                {
                                    
                                    if(System.today() < amendmentObject.Account__r.License_Expiry_Date__c)
                                    {
                                        Decimal daybetweenValue = Decimal.valueof((System.today()).daysbetween(amendmentObject.Account__r.License_Expiry_Date__c)).setScale(2);
                                        tempInvoiceItem.Amount__c = ((tempServiceObject.Price__c / 360) * (daybetweenValue)).round(system.roundingMode.CEILING);
                                        
                                        tempInvoiceItem.Start_Date__c = System.today();
                                        tempInvoiceItem.End_Date__c = amendmentObject.Account__r.License_Expiry_Date__c;    
                                    }
                                    else
                                    {
                                        Decimal daybetweenValue = Decimal.valueof((System.today()).daysbetween(amendmentObject.Account__r.License_Expiry_Date__c.addyears(1))).setScale(2);
                                        tempInvoiceItem.Amount__c = ((tempServiceObject.Price__c / 360) * (daybetweenValue)).round(system.roundingMode.CEILING);
                                        
                                        tempInvoiceItem.Start_Date__c = System.today();
                                        tempInvoiceItem.End_Date__c = amendmentObject.Account__r.License_Expiry_Date__c.addYears(1);
                                    }
                                    
                                }
                                
                                tempInvoiceItem.AFZA_Charges__c = tempServiceObject.AFZA_Charges__c;
                                tempInvoiceItem.AFZA_Other_Charges__c = tempServiceObject.AFZA_Other_Charges__c;
                                tempInvoiceItem.EMPost_Charges__c = tempServiceObject.EMPost_Charges__c;
                                tempInvoiceItem.Local_Government_Charges__c = tempServiceObject.Local_Government_Charges__c;
                                
                                tempInvoiceItem.Maintenance__c = tempServiceObject.Maintenance__c;
                                tempInvoiceItem.Establishment_Card__c = tempServiceObject.Establishment_Card__c;
                                tempInvoiceItem.Etisalat__c = tempServiceObject.Etisalat__c;
                                tempInvoiceItem.Google__c = tempServiceObject.Google__c;
                                tempInvoiceItem.Gov_Establishment_Card__c = tempServiceObject.Gov_Establishment_Card__c;
                                tempInvoiceItem.Sewage__c = tempServiceObject.Sewage__c;
                                tempInvoiceItem.Sign_Board__c = tempServiceObject.Sign_Board__c;
                                tempInvoiceItem.General_Services__c = tempServiceObject.General_Services__c;
                                
                                tempInvoiceItem.AFZA_Maintenance__c = tempServiceObject.AFZA_Maintenance__c;
                                tempInvoiceItem.AFZA_Establishment_Card__c = tempServiceObject.AFZA_Establishment_Card__c;
                                tempInvoiceItem.AFZA_Etisalat__c = tempServiceObject.AFZA_Etisalat__c;
                                tempInvoiceItem.AFZA_Google__c = tempServiceObject.AFZA_Google__c;
                                tempInvoiceItem.AFZA_Sewage__c = tempServiceObject.AFZA_Sewage__c;
                                tempInvoiceItem.AFZA_Sign_Board__c = tempServiceObject.AFZA_Sign_Board__c;
                                tempInvoiceItem.AFZA_General_Services__c = tempServiceObject.AFZA_General_Services__c;
                                
                                tempInvoiceItem.Advertisement_Fee__c = tempServiceObject.Advertisement_Fee__c;
                                tempInvoiceItem.AFZA_Advertisement_Fee__c = tempServiceObject.AFZA_Advertisement_Fee__c;
                                tempInvoiceItem.License_Activity_Fee__c = tempServiceObject.License_Activity_Fee__c;
                                tempInvoiceItem.AFZA_License_Activity_Fee__c = tempServiceObject.AFZA_License_Activity_Fee__c;
                                
                                tempInvoiceItem.Employment_ID_Card__c = tempServiceObject.Employment_ID_Card__c;
                                tempInvoiceItem.AFZA_Employment_ID_Card__c = tempServiceObject.AFZA_Employment_ID_Card__c;                              
                                //Now Add the Invoice Item in List...
                                invoiceLineItemList.add(tempInvoiceItem);
                                
                            }
                        }
                    }
                    
                    
                    
                
                }
                //New Location 3...
                if(amendmentObject.New_Location_3__c != null)
                {
                    //We only neeed to put the Invoice Line Item...
                    tempInvoiceItem = new Invoice_Line_Item__c();
                    //Mapping the Fields...
                    //tempInvoiceItem.Name = 'Tenancy Contract Fee : '+ amendmentObject.New_Location_3_Name__c;
                    tempInvoiceItem.Name = 'Location Rental Rate';
                    tempInvoiceItem.Location__c = amendmentObject.New_Location_3__c;
                    System.debug('The Amendment Object New Location Invoice Item 3 => '+ tempInvoiceItem.Name);
                    decimal tenancyPrice = ((amendmentObject.NLPPD_3__c * amendmentObject.NTCDI__c)).setScale(2);
                    if(tenancyPrice < 0)
                        tenancyPrice = 0;
                    
                    tempInvoiceItem.Start_Date__c = System.today();
                    
                    if(System.Today() < amendmentObject.Account__r.License_Expiry_Date__c)
                        tempInvoiceItem.End_Date__c = amendmentObject.Account__r.License_Expiry_Date__c;
                    else
                        tempInvoiceItem.End_Date__c = amendmentObject.Account__r.License_Expiry_Date__c.addYears(1);
                    
                        
                    tempInvoiceItem.Amount__c = tenancyPrice;
                    
                    tempInvoiceItem.Location_Rental_Rate__c = tenancyPrice;
                    tempInvoiceItem.AFZA_Location_Rental_Rate__c = tenancyPrice;
                    
                    //Package Selection...
                    if(paymentPlanMap.get(amendmentObject.New_Location_Type_3__c) != null)
                    {
                        tempInvoiceItem.Name = 'Location Rental Rate ' + paymentPlanMap.get(amendmentObject.New_Location_Type_3__c).Installment__c + ' Installments';
                        tempInvoiceItem.Location_Rental_Rate__c = tenancyPrice + (tenancyPrice * paymentPlanMap.get(amendmentObject.New_Location_Type_3__c).Sign_Multiplier__c * (paymentPlanMap.get(amendmentObject.New_Location_Type_3__c).Percent__c / 100)    );
                        tempInvoiceItem.AFZA_Location_Rental_Rate__c = tenancyPrice + (tenancyPrice * paymentPlanMap.get(amendmentObject.New_Location_Type_3__c).Sign_Multiplier__c * (paymentPlanMap.get(amendmentObject.New_Location_Type_3__c).Percent__c / 100)    );
                        tempInvoiceItem.Payment_Plan__c = paymentPlanMap.get(amendmentObject.New_Location_Type_3__c).Id;
                        tempInvoiceItem.Package_Value__c = tenancyPrice * paymentPlanMap.get(amendmentObject.New_Location_Type_3__c).Sign_Multiplier__c * (paymentPlanMap.get(amendmentObject.New_Location_Type_3__c).Percent__c / 100);
                        tempInvoiceItem.Installment__c = paymentPlanMap.get(amendmentObject.New_Location_Type_3__c).Installment__c;
                        tempInvoiceItem.Action__c = paymentPlanMap.get(amendmentObject.New_Location_Type_3__c).Action__c;
                        //The Amount...
                        tempInvoiceItem.Amount__c = (tenancyPrice + (tenancyPrice * paymentPlanMap.get(amendmentObject.New_Location_Type_3__c).Sign_Multiplier__c * (paymentPlanMap.get(amendmentObject.New_Location_Type_3__c).Percent__c / 100)    )).setScale(2);
                    }
                    
                    
                    //Now Add the Invoice Item...
                    invoiceLineItemList.add(tempInvoiceItem);
                    
                    //Also doing for the location... 
                    if(servicePriceBookLocationList.size() > 0)
                    {
                        //For the Service Price Book....
                        for(Service_Price_Book__c tempServiceObject : servicePriceBookLocationList)
                        {
                            //Office, Warehouse, Accomodation,Land, E.O.C, Shop...
                            if(tempServiceObject.Location_Type__c == amendmentObject.New_Location_Type_3__c)        
                            {
                                if(tempServiceObject.Fee_Type__c == 'Guarantee')
                                    guaranteeInvoiceFlag = true;
                                    
                                //Now Create a Invoice Line Item for this...
                                tempInvoiceItem = new Invoice_Line_Item__c();
                                //Mapping the Fields....
                                //tempInvoiceItem.Name = bookingObject.Location__r.Name + ' - '+ tempServiceObject.Location_Type__c +' Rental Rate';
                                //tempInvoiceItem.Name = amendmentObject.New_Location_3_Name__c + ' - '+ tempServiceObject.FeeName__c +' Rental Rate';
                                tempInvoiceItem.Name = tempServiceObject.FeeName__c +' Rental Rate';
                                tempInvoiceItem.Fee__c = tempServiceObject.Fee__c;
                                tempInvoiceItem.Location__c =  amendmentObject.New_Location_3__c;
                                tempInvoiceItem.Service_Price_Book__c = tempServiceObject.Id;
                                tempInvoiceItem.Fee_Type__c = tempServiceObject.Fee_Type__c;
                                //tempInvoiceItem.Amount__c = tempServiceObject.Price__c;
                                
                                if(tempServiceObject.locationServiceFlag__c == 'False')
                                    tempInvoiceItem.Amount__c = tempServiceObject.Price__c;
                                else
                                {
                                    if(System.today() < amendmentObject.Account__r.License_Expiry_Date__c)
                                    {
                                        Decimal daybetweenValue = Decimal.valueof((System.today()).daysbetween(amendmentObject.Account__r.License_Expiry_Date__c)).setScale(2);
                                        tempInvoiceItem.Amount__c = ((tempServiceObject.Price__c / 360) * (daybetweenValue)).round(system.roundingMode.CEILING);    
                                        
                                        tempInvoiceItem.Start_Date__c = System.today();
                                        tempInvoiceItem.End_Date__c = amendmentObject.Account__r.License_Expiry_Date__c;
                                        
                                    }
                                    else
                                    {
                                        Decimal daybetweenValue = Decimal.valueof((System.today()).daysbetween(amendmentObject.Account__r.License_Expiry_Date__c.addyears(1))).setScale(2);
                                        tempInvoiceItem.Amount__c = ((tempServiceObject.Price__c / 360) * (daybetweenValue)).round(system.roundingMode.CEILING);
                                        
                                        tempInvoiceItem.Start_Date__c = System.today();
                                        tempInvoiceItem.End_Date__c = amendmentObject.Account__r.License_Expiry_Date__c.addYears(1);
                                        
                                    }
                                    
                                }
                                
                                tempInvoiceItem.AFZA_Charges__c = tempServiceObject.AFZA_Charges__c;
                                tempInvoiceItem.AFZA_Other_Charges__c = tempServiceObject.AFZA_Other_Charges__c;
                                tempInvoiceItem.EMPost_Charges__c = tempServiceObject.EMPost_Charges__c;
                                tempInvoiceItem.Local_Government_Charges__c = tempServiceObject.Local_Government_Charges__c;
                                
                                tempInvoiceItem.Maintenance__c = tempServiceObject.Maintenance__c;
                                tempInvoiceItem.Establishment_Card__c = tempServiceObject.Establishment_Card__c;
                                tempInvoiceItem.Etisalat__c = tempServiceObject.Etisalat__c;
                                tempInvoiceItem.Google__c = tempServiceObject.Google__c;
                                tempInvoiceItem.Gov_Establishment_Card__c = tempServiceObject.Gov_Establishment_Card__c;
                                tempInvoiceItem.Sewage__c = tempServiceObject.Sewage__c;
                                tempInvoiceItem.Sign_Board__c = tempServiceObject.Sign_Board__c;
                                tempInvoiceItem.General_Services__c = tempServiceObject.General_Services__c;
                                
                                tempInvoiceItem.AFZA_Maintenance__c = tempServiceObject.AFZA_Maintenance__c;
                                tempInvoiceItem.AFZA_Establishment_Card__c = tempServiceObject.AFZA_Establishment_Card__c;
                                tempInvoiceItem.AFZA_Etisalat__c = tempServiceObject.AFZA_Etisalat__c;
                                tempInvoiceItem.AFZA_Google__c = tempServiceObject.AFZA_Google__c;
                                tempInvoiceItem.AFZA_Sewage__c = tempServiceObject.AFZA_Sewage__c;
                                tempInvoiceItem.AFZA_Sign_Board__c = tempServiceObject.AFZA_Sign_Board__c;
                                tempInvoiceItem.AFZA_General_Services__c = tempServiceObject.AFZA_General_Services__c;
                                
                                tempInvoiceItem.Advertisement_Fee__c = tempServiceObject.Advertisement_Fee__c;
                                tempInvoiceItem.AFZA_Advertisement_Fee__c = tempServiceObject.AFZA_Advertisement_Fee__c;
                                tempInvoiceItem.License_Activity_Fee__c = tempServiceObject.License_Activity_Fee__c;
                                tempInvoiceItem.AFZA_License_Activity_Fee__c = tempServiceObject.AFZA_License_Activity_Fee__c;
                                
                                tempInvoiceItem.Employment_ID_Card__c = tempServiceObject.Employment_ID_Card__c;
                                tempInvoiceItem.AFZA_Employment_ID_Card__c = tempServiceObject.AFZA_Employment_ID_Card__c;
                                //Now Add the Invoice Item in List...
                                invoiceLineItemList.add(tempInvoiceItem);
                                
                            }
                        }
                    }
                
                }
                
                
                
            }
            else if(recordTypeName == 'Company Amendment - Change License Type')
            {
                //Account_License__c,New_License_Type__c
                //Change Location Flat Fee...
                
                List<Service_Price_Book__c> feeObject = new List<Service_Price_Book__c>([Select Employment_ID_Card__c,AFZA_Employment_ID_Card__c,Advertisement_Fee__c,AFZA_Advertisement_Fee__c,AFZA_Establishment_Card__c,AFZA_Etisalat__c,AFZA_General_Services__c,AFZA_Google__c,AFZA_License_Activity_Fee__c,License_Activity_Fee__c,AFZA_Maintenance__c,AFZA_Sign_Board__c,AFZA_Sewage__c,General_Services__c, Establishment_Card__c, Etisalat__c, Google__c, Gov_Establishment_Card__c, Maintenance__c, Sewage__c, Sign_Board__c,AFZA_Charges__c, AFZA_Other_Charges__c,  EMPost_Charges__c, Local_Government_Charges__c,FeeName__c,Price__c, License_Application_Type__c, License_App_Category__c, Id, Fee__c, Fee_Type__c From Service_Price_Book__c where License_Application_Type__c =: 'Amendment' AND License_App_Category__c =: amendmentObject.Account_License__c AND Fee_Type__c =: 'Amendment - Change License Type Fee' AND isActive__c =: true]);
                //We only neeed to put the Invoice Line Item...
                tempInvoiceItem = new Invoice_Line_Item__c();
                
                if(feeObject.size() > 0)
                {
                    //We only neeed to put the Invoice Line Item...
                    tempInvoiceItem = new Invoice_Line_Item__c();    
                    //Mapping the Fields...
                    //tempInvoiceItem.Name = feeObject[0].Name;
                    tempInvoiceItem.Name = feeObject[0].FeeName__c;
                    //tempInvoiceItem.Fee__c = feeObject[0].Id;
                    tempInvoiceItem.Fee__c = feeObject[0].Fee__c;
                    tempInvoiceItem.Service_Price_Book__c = feeObject[0].Id;
                    tempInvoiceItem.Fee_Type__c = feeObject[0].Fee_Type__c;
                    tempInvoiceItem.Amount__c = feeObject[0].Price__c;
                    tempInvoiceItem.Amendment_Charges__c = feeObject[0].Price__c;
                    tempInvoiceItem.AFZA_Amendment_Charges__c = feeObject[0].Price__c;
                    
                    tempInvoiceItem.AFZA_Charges__c = feeObject[0].AFZA_Charges__c;
                    tempInvoiceItem.AFZA_Other_Charges__c = feeObject[0].AFZA_Other_Charges__c;
                    tempInvoiceItem.EMPost_Charges__c = feeObject[0].EMPost_Charges__c;
                    tempInvoiceItem.Local_Government_Charges__c = feeObject[0].Local_Government_Charges__c;
                    
                    tempInvoiceItem.Maintenance__c = feeObject[0].Maintenance__c;
                    tempInvoiceItem.Establishment_Card__c = feeObject[0].Establishment_Card__c;
                    tempInvoiceItem.Etisalat__c = feeObject[0].Etisalat__c;
                    tempInvoiceItem.Google__c = feeObject[0].Google__c;
                    tempInvoiceItem.Gov_Establishment_Card__c = feeObject[0].Gov_Establishment_Card__c;
                    tempInvoiceItem.Sewage__c = feeObject[0].Sewage__c;
                    tempInvoiceItem.Sign_Board__c = feeObject[0].Sign_Board__c;
                    tempInvoiceItem.General_Services__c = feeObject[0].General_Services__c;
                    
                    tempInvoiceItem.AFZA_Maintenance__c = feeObject[0].AFZA_Maintenance__c;
                    tempInvoiceItem.AFZA_Establishment_Card__c = feeObject[0].AFZA_Establishment_Card__c;
                    tempInvoiceItem.AFZA_Etisalat__c = feeObject[0].AFZA_Etisalat__c;
                    tempInvoiceItem.AFZA_Google__c = feeObject[0].AFZA_Google__c;
                    tempInvoiceItem.AFZA_Sewage__c = feeObject[0].AFZA_Sewage__c;
                    tempInvoiceItem.AFZA_Sign_Board__c = feeObject[0].AFZA_Sign_Board__c;
                    tempInvoiceItem.AFZA_General_Services__c = feeObject[0].AFZA_General_Services__c;
                    
                    tempInvoiceItem.Advertisement_Fee__c = feeObject[0].Advertisement_Fee__c;
                    tempInvoiceItem.AFZA_Advertisement_Fee__c = feeObject[0].AFZA_Advertisement_Fee__c;
                    tempInvoiceItem.License_Activity_Fee__c = feeObject[0].License_Activity_Fee__c;
                    tempInvoiceItem.AFZA_License_Activity_Fee__c = feeObject[0].AFZA_License_Activity_Fee__c;
                    
                    tempInvoiceItem.Employment_ID_Card__c = feeObject[0].Employment_ID_Card__c;
                    tempInvoiceItem.AFZA_Employment_ID_Card__c = feeObject[0].AFZA_Employment_ID_Card__c;                    
                    
                    
                    //Now Add the Invoice Item... (2/October/2012)
                    //invoiceLineItemList.add(tempInvoiceItem);
                }
                 
                //Now For License Type Difference Fee...
                if(amendmentObject.Account_License__c == 'Trading - One Activity' && amendmentObject.New_License_Type__c == 'Trading - Two Activity')
                {
                    List<Service_Price_Book__c> feeObject1 = new List<Service_Price_Book__c>([Select General_Services__c, Establishment_Card__c, Etisalat__c, Google__c, Gov_Establishment_Card__c, Maintenance__c, Sewage__c, Sign_Board__c,AFZA_Charges__c, AFZA_Other_Charges__c,    EMPost_Charges__c, Local_Government_Charges__c,FeeName__c,Price__c, License_Application_Type__c, License_App_Category__c, Id, Fee__c, Fee_Type__c From Service_Price_Book__c where License_Application_Type__c =: 'Amendment' AND License_App_Category__c =: amendmentObject.Account_License__c AND Fee_Type__c =: 'Activity1Activty2 Fee'  AND isActive__c =: true order by CreatedDate limit 1]);
                
                    if(feeObject1.size() > 0)
                    {
                        tempInvoiceItem.License_Difference_Fee__c = feeObject1[0].Price__c;
                        tempInvoiceItem.Amount__c = tempInvoiceItem.Amount__c + tempInvoiceItem.License_Difference_Fee__c;
                        tempInvoiceItem.Amendment_Charges__c = tempInvoiceItem.Amount__c + tempInvoiceItem.License_Difference_Fee__c;
                        //tempInvoiceItem.AFZA_Charges__c = tempInvoiceItem.AFZA_Charges__c + tempInvoiceItem.License_Difference_Fee__c;
                        tempInvoiceItem.AFZA_Amendment_Charges__c = tempInvoiceItem.AFZA_Amendment_Charges__c + tempInvoiceItem.License_Difference_Fee__c;
                        
                        /*//We only neeed to put the Invoice Line Item...
                        tempInvoiceItem = new Invoice_Line_Item__c();
                        //Mapping the Fields...
                        //tempInvoiceItem.Name = feeObject1[0].Name;
                        tempInvoiceItem.Name = feeObject1[0].FeeName__c;
                        //tempInvoiceItem.Fee__c = feeObject1[0].Id;
                        tempInvoiceItem.Fee__c = feeObject1[0].Fee__c;
                        tempInvoiceItem.Service_Price_Book__c = feeObject1[0].Id;
                        tempInvoiceItem.Fee_Type__c = feeObject1[0].Fee_Type__c;
                        tempInvoiceItem.Amount__c = feeObject1[0].Price__c;
                        
                        tempInvoiceItem.AFZA_Charges__c = feeObject1[0].AFZA_Charges__c;
                        tempInvoiceItem.AFZA_Other_Charges__c = feeObject1[0].AFZA_Other_Charges__c;
                        tempInvoiceItem.EMPost_Charges__c = feeObject1[0].EMPost_Charges__c;
                        tempInvoiceItem.Local_Government_Charges__c = feeObject1[0].Local_Government_Charges__c;
                        
                        tempInvoiceItem.Maintenance__c = feeObject1[0].Maintenance__c;
                        tempInvoiceItem.Establishment_Card__c = feeObject1[0].Establishment_Card__c;
                        tempInvoiceItem.Etisalat__c = feeObject1[0].Etisalat__c;
                        tempInvoiceItem.Google__c = feeObject1[0].Google__c;
                        tempInvoiceItem.Gov_Establishment_Card__c = feeObject1[0].Gov_Establishment_Card__c;
                        tempInvoiceItem.Sewage__c = feeObject1[0].Sewage__c;
                        tempInvoiceItem.Sign_Board__c = feeObject1[0].Sign_Board__c;
                        tempInvoiceItem.General_Services__c = feeObject1[0].General_Services__c;
                        
                        
                        //Now Add the Invoice Item...
                        invoiceLineItemList.add(tempInvoiceItem);*/
                    }
                    
                   
                
                }
                else if(amendmentObject.Account_License__c == 'Trading - One Activity' && amendmentObject.New_License_Type__c == 'Trading - Three Activity')
                {
                    System.debug('Inside the Trading One Activity License Difference to Trading 3 License Difference ');
                    
                    List<Service_Price_Book__c> feeObject2 = new List<Service_Price_Book__c>([Select General_Services__c, Establishment_Card__c, Etisalat__c, Google__c, Gov_Establishment_Card__c, Maintenance__c, Sewage__c, Sign_Board__c,AFZA_Charges__c, AFZA_Other_Charges__c,    EMPost_Charges__c, Local_Government_Charges__c,FeeName__c,Price__c, License_Application_Type__c, License_App_Category__c, Id, Fee__c, Fee_Type__c From Service_Price_Book__c where License_Application_Type__c =: 'Amendment' AND License_App_Category__c =: amendmentObject.Account_License__c AND Fee_Type__c =: 'Activity1Activity3 Fee' AND isActive__c =: true order by CreatedDate limit 1]);
                
                    if(feeObject2.size() > 0)
                    {
                        tempInvoiceItem.License_Difference_Fee__c = feeObject2[0].Price__c;
                        tempInvoiceItem.Amount__c = tempInvoiceItem.Amount__c + tempInvoiceItem.License_Difference_Fee__c;
                        tempInvoiceItem.Amendment_Charges__c = tempInvoiceItem.Amount__c + tempInvoiceItem.License_Difference_Fee__c;
                        //tempInvoiceItem.AFZA_Charges__c = tempInvoiceItem.AFZA_Charges__c + tempInvoiceItem.License_Difference_Fee__c;
                        tempInvoiceItem.AFZA_Amendment_Charges__c = tempInvoiceItem.AFZA_Amendment_Charges__c + tempInvoiceItem.License_Difference_Fee__c;
                        
                        /*//We only neeed to put the Invoice Line Item...
                        tempInvoiceItem = new Invoice_Line_Item__c();
                        //Mapping the Fields...
                        //tempInvoiceItem.Name = feeObject2[0].Name;
                        tempInvoiceItem.Name = feeObject2[0].FeeName__c;
                        //tempInvoiceItem.Fee__c = feeObject2[0].Id;
                        tempInvoiceItem.Fee__c = feeObject2[0].Fee__c;
                        tempInvoiceItem.Service_Price_Book__c = feeObject2[0].Id;
                        tempInvoiceItem.Fee_Type__c = feeObject2[0].Fee_Type__c;
                        tempInvoiceItem.Amount__c = feeObject2[0].Price__c;
                        
                        tempInvoiceItem.AFZA_Charges__c = feeObject2[0].AFZA_Charges__c;
                        tempInvoiceItem.AFZA_Other_Charges__c = feeObject2[0].AFZA_Other_Charges__c;
                        tempInvoiceItem.EMPost_Charges__c = feeObject2[0].EMPost_Charges__c;
                        tempInvoiceItem.Local_Government_Charges__c = feeObject2[0].Local_Government_Charges__c;
                                                
                        tempInvoiceItem.Maintenance__c = feeObject2[0].Maintenance__c;
                        tempInvoiceItem.Establishment_Card__c = feeObject2[0].Establishment_Card__c;
                        tempInvoiceItem.Etisalat__c = feeObject2[0].Etisalat__c;
                        tempInvoiceItem.Google__c = feeObject2[0].Google__c;
                        tempInvoiceItem.Gov_Establishment_Card__c = feeObject2[0].Gov_Establishment_Card__c;
                        tempInvoiceItem.Sewage__c = feeObject2[0].Sewage__c;
                        tempInvoiceItem.Sign_Board__c = feeObject2[0].Sign_Board__c;
                        tempInvoiceItem.General_Services__c = feeObject2[0].General_Services__c;
                        
                        //Now Add the Invoice Item...
                        invoiceLineItemList.add(tempInvoiceItem);*/
                    }
                   
                    
                }
                else if(amendmentObject.Account_License__c == 'Trading - One Activity' && amendmentObject.New_License_Type__c == 'Trading - General Trading')
                {
                    List<Service_Price_Book__c> feeObject3 = new List<Service_Price_Book__c>([Select General_Services__c, Establishment_Card__c, Etisalat__c, Google__c, Gov_Establishment_Card__c, Maintenance__c, Sewage__c, Sign_Board__c,AFZA_Charges__c, AFZA_Other_Charges__c,EMPost_Charges__c, Local_Government_Charges__c,FeeName__c,Price__c, License_Application_Type__c, License_App_Category__c, Id, Fee__c, Fee_Type__c From Service_Price_Book__c where License_Application_Type__c =: 'Amendment' AND License_App_Category__c =: amendmentObject.Account_License__c AND Fee_Type__c =: 'Activity1General Fee' AND isActive__c =: true order by CreatedDate limit 1]);
                    
                    if(feeObject3.size() > 0)
                    {
                        tempInvoiceItem.License_Difference_Fee__c = feeObject3[0].Price__c;
                        tempInvoiceItem.Amount__c = tempInvoiceItem.Amount__c + tempInvoiceItem.License_Difference_Fee__c;
                        tempInvoiceItem.Amendment_Charges__c = tempInvoiceItem.Amount__c + tempInvoiceItem.License_Difference_Fee__c;
                        //tempInvoiceItem.AFZA_Charges__c = tempInvoiceItem.AFZA_Charges__c + tempInvoiceItem.License_Difference_Fee__c;
                        tempInvoiceItem.AFZA_Amendment_Charges__c = tempInvoiceItem.AFZA_Amendment_Charges__c + tempInvoiceItem.License_Difference_Fee__c;
                        
                        /*//We only neeed to put the Invoice Line Item...
                        tempInvoiceItem = new Invoice_Line_Item__c();
                        //Mapping the Fields...
                        //tempInvoiceItem.Name = feeObject3[0].Name;
                        tempInvoiceItem.Name = feeObject3[0].FeeName__c;
                        //tempInvoiceItem.Fee__c = feeObject3[0].Id;
                        tempInvoiceItem.Fee__c = feeObject3[0].Fee__c;
                        tempInvoiceItem.Service_Price_Book__c = feeObject3[0].Id;
                        tempInvoiceItem.Fee_Type__c = feeObject3[0].Fee_Type__c;
                        tempInvoiceItem.Amount__c = feeObject3[0].Price__c;
                        
                        tempInvoiceItem.AFZA_Charges__c = feeObject3[0].AFZA_Charges__c;
                        tempInvoiceItem.AFZA_Other_Charges__c = feeObject3[0].AFZA_Other_Charges__c;
                        tempInvoiceItem.EMPost_Charges__c = feeObject3[0].EMPost_Charges__c;
                        tempInvoiceItem.Local_Government_Charges__c = feeObject3[0].Local_Government_Charges__c;
                        
                        tempInvoiceItem.Maintenance__c = feeObject3[0].Maintenance__c;
                        tempInvoiceItem.Establishment_Card__c = feeObject3[0].Establishment_Card__c;
                        tempInvoiceItem.Etisalat__c = feeObject3[0].Etisalat__c;
                        tempInvoiceItem.Google__c = feeObject3[0].Google__c;
                        tempInvoiceItem.Gov_Establishment_Card__c = feeObject3[0].Gov_Establishment_Card__c;
                        tempInvoiceItem.Sewage__c = feeObject3[0].Sewage__c;
                        tempInvoiceItem.Sign_Board__c = feeObject3[0].Sign_Board__c;
                        tempInvoiceItem.General_Services__c = feeObject3[0].General_Services__c;
                        
                        //Now Add the Invoice Item...
                        invoiceLineItemList.add(tempInvoiceItem);*/   
                    }
                    
                }
                else if(amendmentObject.Account_License__c == 'Trading - One Activity' && amendmentObject.New_License_Type__c == 'Services')
                {
                    
                    List<Service_Price_Book__c> feeObject4 = new List<Service_Price_Book__c>([Select General_Services__c, Establishment_Card__c, Etisalat__c, Google__c, Gov_Establishment_Card__c, Maintenance__c, Sewage__c, Sign_Board__c,AFZA_Charges__c, AFZA_Other_Charges__c,EMPost_Charges__c, Local_Government_Charges__c,FeeName__c,Price__c, License_Application_Type__c, License_App_Category__c, Id, Fee__c, Fee_Type__c From Service_Price_Book__c where License_Application_Type__c =: 'Amendment' AND License_App_Category__c =: amendmentObject.Account_License__c AND Fee_Type__c =: 'Activity1Services Fee' AND isActive__c =: true order by CreatedDate limit 1]);
                    
                    if(feeObject4.size() > 0)
                    {
                        tempInvoiceItem.License_Difference_Fee__c = feeObject4[0].Price__c;
                        tempInvoiceItem.Amount__c = tempInvoiceItem.Amount__c + tempInvoiceItem.License_Difference_Fee__c;
                        tempInvoiceItem.Amendment_Charges__c = tempInvoiceItem.Amount__c + tempInvoiceItem.License_Difference_Fee__c;
                        //tempInvoiceItem.AFZA_Charges__c = tempInvoiceItem.AFZA_Charges__c + tempInvoiceItem.License_Difference_Fee__c;
                        tempInvoiceItem.AFZA_Amendment_Charges__c = tempInvoiceItem.AFZA_Amendment_Charges__c + tempInvoiceItem.License_Difference_Fee__c;
                        /*//We only neeed to put the Invoice Line Item...
                        tempInvoiceItem = new Invoice_Line_Item__c();
                        //Mapping the Fields...
                        //tempInvoiceItem.Name = feeObject4[0].Name;
                        tempInvoiceItem.Name = feeObject4[0].FeeName__c;
                        //tempInvoiceItem.Fee__c = feeObject4[0].Id;
                        tempInvoiceItem.Fee__c = feeObject4[0].Fee__c;
                        tempInvoiceItem.Service_Price_Book__c = feeObject4[0].Id;
                        tempInvoiceItem.Fee_Type__c = feeObject4[0].Fee_Type__c;
                        tempInvoiceItem.Amount__c = feeObject4[0].Price__c;
                        
                        tempInvoiceItem.AFZA_Charges__c = feeObject4[0].AFZA_Charges__c;
                        tempInvoiceItem.AFZA_Other_Charges__c = feeObject4[0].AFZA_Other_Charges__c;
                        tempInvoiceItem.EMPost_Charges__c = feeObject4[0].EMPost_Charges__c;
                        tempInvoiceItem.Local_Government_Charges__c = feeObject4[0].Local_Government_Charges__c;   
                        
                        tempInvoiceItem.Maintenance__c = feeObject4[0].Maintenance__c;
                        tempInvoiceItem.Establishment_Card__c = feeObject4[0].Establishment_Card__c;
                        tempInvoiceItem.Etisalat__c = feeObject4[0].Etisalat__c;
                        tempInvoiceItem.Google__c = feeObject4[0].Google__c;
                        tempInvoiceItem.Gov_Establishment_Card__c = feeObject4[0].Gov_Establishment_Card__c;
                        tempInvoiceItem.Sewage__c = feeObject4[0].Sewage__c;
                        tempInvoiceItem.Sign_Board__c = feeObject4[0].Sign_Board__c;
                        tempInvoiceItem.General_Services__c = feeObject4[0].General_Services__c;
                        
                        //Now Add the Invoice Item...
                        invoiceLineItemList.add(tempInvoiceItem);*/   
                    
                    }
                    
                    
                }
                else if(amendmentObject.Account_License__c == 'Trading - One Activity' && amendmentObject.New_License_Type__c == 'Industrial')
                {
                    
                    List<Service_Price_Book__c> feeObject5 = new List<Service_Price_Book__c>([Select General_Services__c, Establishment_Card__c, Etisalat__c, Google__c, Gov_Establishment_Card__c, Maintenance__c, Sewage__c, Sign_Board__c,AFZA_Charges__c, AFZA_Other_Charges__c,EMPost_Charges__c, Local_Government_Charges__c,FeeName__c,Price__c, License_Application_Type__c, License_App_Category__c, Id, Fee__c, Fee_Type__c From Service_Price_Book__c where License_Application_Type__c =: 'Amendment' AND License_App_Category__c =: amendmentObject.Account_License__c AND Fee_Type__c =: 'Activity1Industrial Fee' AND isActive__c =: true order by CreatedDate limit 1]);
                    
                    if(feeObject5.size() > 0)
                    {
                        tempInvoiceItem.License_Difference_Fee__c = feeObject5[0].Price__c;
                        tempInvoiceItem.Amount__c = tempInvoiceItem.Amount__c + tempInvoiceItem.License_Difference_Fee__c;
                        tempInvoiceItem.Amendment_Charges__c = tempInvoiceItem.Amount__c + tempInvoiceItem.License_Difference_Fee__c;
                        //tempInvoiceItem.AFZA_Charges__c = tempInvoiceItem.AFZA_Charges__c + tempInvoiceItem.License_Difference_Fee__c;
                        tempInvoiceItem.AFZA_Amendment_Charges__c = tempInvoiceItem.AFZA_Amendment_Charges__c + tempInvoiceItem.License_Difference_Fee__c;
                        /*//We only neeed to put the Invoice Line Item...
                        tempInvoiceItem = new Invoice_Line_Item__c();
                        //Mapping the Fields...
                        //tempInvoiceItem.Name = feeObject5[0].Name;
                        tempInvoiceItem.Name = feeObject5[0].FeeName__c;
                        //tempInvoiceItem.Fee__c = feeObject5[0].Id;
                        tempInvoiceItem.Fee__c = feeObject5[0].Fee__c;
                        tempInvoiceItem.Service_Price_Book__c = feeObject5[0].Id;
                        tempInvoiceItem.Fee_Type__c = feeObject5[0].Fee_Type__c;
                        tempInvoiceItem.Amount__c = feeObject5[0].Price__c;
                        
                        tempInvoiceItem.AFZA_Charges__c = feeObject5[0].AFZA_Charges__c;
                        tempInvoiceItem.AFZA_Other_Charges__c = feeObject5[0].AFZA_Other_Charges__c;
                        tempInvoiceItem.EMPost_Charges__c = feeObject5[0].EMPost_Charges__c;
                        tempInvoiceItem.Local_Government_Charges__c = feeObject5[0].Local_Government_Charges__c;
                        
                        tempInvoiceItem.Maintenance__c = feeObject5[0].Maintenance__c;
                        tempInvoiceItem.Establishment_Card__c = feeObject5[0].Establishment_Card__c;
                        tempInvoiceItem.Etisalat__c = feeObject5[0].Etisalat__c;
                        tempInvoiceItem.Google__c = feeObject5[0].Google__c;
                        tempInvoiceItem.Gov_Establishment_Card__c = feeObject5[0].Gov_Establishment_Card__c;
                        tempInvoiceItem.Sewage__c = feeObject5[0].Sewage__c;
                        tempInvoiceItem.Sign_Board__c = feeObject5[0].Sign_Board__c;
                        tempInvoiceItem.General_Services__c = feeObject5[0].General_Services__c;
                        
                        //Now Add the Invoice Item...
                        invoiceLineItemList.add(tempInvoiceItem);*/
                    }
                    
                }
                else if(amendmentObject.Account_License__c == 'Trading - Two Activity' && amendmentObject.New_License_Type__c == 'Trading - Three Activity')
                {
                    
                    List<Service_Price_Book__c> feeObject6 = new List<Service_Price_Book__c>([Select General_Services__c, Establishment_Card__c, Etisalat__c, Google__c, Gov_Establishment_Card__c, Maintenance__c, Sewage__c, Sign_Board__c,AFZA_Charges__c, AFZA_Other_Charges__c,EMPost_Charges__c, Local_Government_Charges__c,FeeName__c,Price__c, License_Application_Type__c, License_App_Category__c, Id, Fee__c, Fee_Type__c From Service_Price_Book__c where License_Application_Type__c =: 'Amendment' AND License_App_Category__c =: amendmentObject.Account_License__c AND Fee_Type__c =: 'Activity2Activity3 Fee' AND isActive__c =: true order by CreatedDate limit 1]);
                    
                    if(feeObject6.size() > 0)
                    {
                        tempInvoiceItem.License_Difference_Fee__c = feeObject6[0].Price__c;
                        tempInvoiceItem.Amount__c = tempInvoiceItem.Amount__c + tempInvoiceItem.License_Difference_Fee__c;
                        tempInvoiceItem.Amendment_Charges__c = tempInvoiceItem.Amount__c + tempInvoiceItem.License_Difference_Fee__c;
                        //tempInvoiceItem.AFZA_Charges__c = tempInvoiceItem.AFZA_Charges__c + tempInvoiceItem.License_Difference_Fee__c;
                        tempInvoiceItem.AFZA_Amendment_Charges__c = tempInvoiceItem.AFZA_Amendment_Charges__c + tempInvoiceItem.License_Difference_Fee__c;
                        
                        /*//We only neeed to put the Invoice Line Item...
                        tempInvoiceItem = new Invoice_Line_Item__c();
                        //Mapping the Fields...
                        //tempInvoiceItem.Name = feeObject6[0].Name;
                        tempInvoiceItem.Name = feeObject6[0].FeeName__c;
                        //tempInvoiceItem.Fee__c = feeObject6[0].Id;
                        tempInvoiceItem.Fee__c = feeObject6[0].Fee__c;
                        tempInvoiceItem.Service_Price_Book__c = feeObject6[0].Id;
                        tempInvoiceItem.Fee_Type__c = feeObject6[0].Fee_Type__c;
                        tempInvoiceItem.Amount__c = feeObject6[0].Price__c;
                        
                        tempInvoiceItem.AFZA_Charges__c = feeObject6[0].AFZA_Charges__c;
                        tempInvoiceItem.AFZA_Other_Charges__c = feeObject6[0].AFZA_Other_Charges__c;
                        tempInvoiceItem.EMPost_Charges__c = feeObject6[0].EMPost_Charges__c;
                        tempInvoiceItem.Local_Government_Charges__c = feeObject6[0].Local_Government_Charges__c;
                        
                        tempInvoiceItem.Maintenance__c = feeObject6[0].Maintenance__c;
                        tempInvoiceItem.Establishment_Card__c = feeObject6[0].Establishment_Card__c;
                        tempInvoiceItem.Etisalat__c = feeObject6[0].Etisalat__c;
                        tempInvoiceItem.Google__c = feeObject6[0].Google__c;
                        tempInvoiceItem.Gov_Establishment_Card__c = feeObject6[0].Gov_Establishment_Card__c;
                        tempInvoiceItem.Sewage__c = feeObject6[0].Sewage__c;
                        tempInvoiceItem.Sign_Board__c = feeObject6[0].Sign_Board__c;
                        tempInvoiceItem.General_Services__c = feeObject6[0].General_Services__c;
                        
                        //Now Add the Invoice Item...
                        invoiceLineItemList.add(tempInvoiceItem);*/   
                    
                    }
                    
                }
                else if(amendmentObject.Account_License__c == 'Trading - Two Activity' && amendmentObject.New_License_Type__c == 'Trading - General Trading')
                {
                    
                    List<Service_Price_Book__c> feeObject7 = new List<Service_Price_Book__c>([Select General_Services__c, Establishment_Card__c, Etisalat__c, Google__c, Gov_Establishment_Card__c, Maintenance__c, Sewage__c, Sign_Board__c,AFZA_Charges__c, AFZA_Other_Charges__c,EMPost_Charges__c, Local_Government_Charges__c,FeeName__c,Price__c, License_Application_Type__c, License_App_Category__c, Id, Fee__c, Fee_Type__c From Service_Price_Book__c where License_Application_Type__c =: 'Amendment' AND License_App_Category__c =: amendmentObject.Account_License__c AND Fee_Type__c =: 'Activity2General Fee' AND isActive__c =: true order by CreatedDate limit 1]);
                    
                    if(feeObject7.size() > 0)
                    {
                        tempInvoiceItem.License_Difference_Fee__c = feeObject7[0].Price__c;
                        tempInvoiceItem.Amount__c = tempInvoiceItem.Amount__c + tempInvoiceItem.License_Difference_Fee__c;
                        tempInvoiceItem.Amendment_Charges__c = tempInvoiceItem.Amount__c + tempInvoiceItem.License_Difference_Fee__c;
                        //tempInvoiceItem.AFZA_Charges__c = tempInvoiceItem.AFZA_Charges__c + tempInvoiceItem.License_Difference_Fee__c;
                        tempInvoiceItem.AFZA_Amendment_Charges__c = tempInvoiceItem.AFZA_Amendment_Charges__c + tempInvoiceItem.License_Difference_Fee__c;
                        /*//We only neeed to put the Invoice Line Item...
                        tempInvoiceItem = new Invoice_Line_Item__c();
                        //Mapping the Fields...
                        //tempInvoiceItem.Name = feeObject7[0].Name;
                        tempInvoiceItem.Name = feeObject7[0].FeeName__c;
                        //tempInvoiceItem.Fee__c = feeObject7[0].Id;
                        tempInvoiceItem.Fee__c = feeObject7[0].Fee__c;
                        tempInvoiceItem.Service_Price_Book__c = feeObject7[0].Id;
                        tempInvoiceItem.Fee_Type__c = feeObject7[0].Fee_Type__c;
                        tempInvoiceItem.Amount__c = feeObject7[0].Price__c;
                        
                        tempInvoiceItem.AFZA_Charges__c = feeObject7[0].AFZA_Charges__c;
                        tempInvoiceItem.AFZA_Other_Charges__c = feeObject7[0].AFZA_Other_Charges__c;
                        tempInvoiceItem.EMPost_Charges__c = feeObject7[0].EMPost_Charges__c;
                        tempInvoiceItem.Local_Government_Charges__c = feeObject7[0].Local_Government_Charges__c;
                        
                        tempInvoiceItem.Maintenance__c = feeObject7[0].Maintenance__c;
                        tempInvoiceItem.Establishment_Card__c = feeObject7[0].Establishment_Card__c;
                        tempInvoiceItem.Etisalat__c = feeObject7[0].Etisalat__c;
                        tempInvoiceItem.Google__c = feeObject7[0].Google__c;
                        tempInvoiceItem.Gov_Establishment_Card__c = feeObject7[0].Gov_Establishment_Card__c;
                        tempInvoiceItem.Sewage__c = feeObject7[0].Sewage__c;
                        tempInvoiceItem.Sign_Board__c = feeObject7[0].Sign_Board__c;
                        tempInvoiceItem.General_Services__c = feeObject7[0].General_Services__c;
                        
                        //Now Add the Invoice Item...
                        invoiceLineItemList.add(tempInvoiceItem);*/
                    }
                    
                    
                }
                else if(amendmentObject.Account_License__c == 'Trading - Two Activity' && amendmentObject.New_License_Type__c == 'Services')
                {
                    List<Service_Price_Book__c> feeObject8 = new List<Service_Price_Book__c>([Select General_Services__c, Establishment_Card__c, Etisalat__c, Google__c, Gov_Establishment_Card__c, Maintenance__c, Sewage__c, Sign_Board__c,AFZA_Charges__c, AFZA_Other_Charges__c,EMPost_Charges__c, Local_Government_Charges__c,FeeName__c,Price__c, License_Application_Type__c, License_App_Category__c, Id, Fee__c, Fee_Type__c From Service_Price_Book__c where License_Application_Type__c =: 'Amendment' AND License_App_Category__c =: amendmentObject.Account_License__c AND Fee_Type__c =: 'Activity2Services Fee' AND isActive__c =: true order by CreatedDate limit 1]);
                    
                    if(feeObject8.size() > 0)
                    {
                        tempInvoiceItem.License_Difference_Fee__c = feeObject8[0].Price__c;
                        tempInvoiceItem.Amount__c = tempInvoiceItem.Amount__c + tempInvoiceItem.License_Difference_Fee__c;
                        tempInvoiceItem.Amendment_Charges__c = tempInvoiceItem.Amount__c + tempInvoiceItem.License_Difference_Fee__c;
                        //tempInvoiceItem.AFZA_Charges__c = tempInvoiceItem.AFZA_Charges__c + tempInvoiceItem.License_Difference_Fee__c;
                        tempInvoiceItem.AFZA_Amendment_Charges__c = tempInvoiceItem.AFZA_Amendment_Charges__c + tempInvoiceItem.License_Difference_Fee__c;
                        
                        /*//We only neeed to put the Invoice Line Item...
                        tempInvoiceItem = new Invoice_Line_Item__c();
                        //Mapping the Fields...
                        //tempInvoiceItem.Name = feeObject8[0].Name;
                        tempInvoiceItem.Name = feeObject8[0].FeeName__c;
                        //tempInvoiceItem.Fee__c = feeObject8[0].Id;
                        tempInvoiceItem.Fee__c = feeObject8[0].Fee__c;
                        tempInvoiceItem.Service_Price_Book__c = feeObject8[0].Id;
                        tempInvoiceItem.Fee_Type__c = feeObject8[0].Fee_Type__c;
                        tempInvoiceItem.Amount__c = feeObject8[0].Price__c;
                        
                        tempInvoiceItem.AFZA_Charges__c = feeObject8[0].AFZA_Charges__c;
                        tempInvoiceItem.AFZA_Other_Charges__c = feeObject8[0].AFZA_Other_Charges__c;
                        tempInvoiceItem.EMPost_Charges__c = feeObject8[0].EMPost_Charges__c;
                        tempInvoiceItem.Local_Government_Charges__c = feeObject8[0].Local_Government_Charges__c;
                        
                        tempInvoiceItem.Maintenance__c = feeObject8[0].Maintenance__c;
                        tempInvoiceItem.Establishment_Card__c = feeObject8[0].Establishment_Card__c;
                        tempInvoiceItem.Etisalat__c = feeObject8[0].Etisalat__c;
                        tempInvoiceItem.Google__c = feeObject8[0].Google__c;
                        tempInvoiceItem.Gov_Establishment_Card__c = feeObject8[0].Gov_Establishment_Card__c;
                        tempInvoiceItem.Sewage__c = feeObject8[0].Sewage__c;
                        tempInvoiceItem.Sign_Board__c = feeObject8[0].Sign_Board__c;
                        tempInvoiceItem.General_Services__c = feeObject8[0].General_Services__c;
                        
                        //Now Add the Invoice Item...
                        invoiceLineItemList.add(tempInvoiceItem);*/
                        
                    }
                    
                }
                else if(amendmentObject.Account_License__c == 'Trading - Two Activity' && amendmentObject.New_License_Type__c == 'Industrial')
                {
                    List<Service_Price_Book__c> feeObject9 = new List<Service_Price_Book__c>([Select General_Services__c, Establishment_Card__c, Etisalat__c, Google__c, Gov_Establishment_Card__c, Maintenance__c, Sewage__c, Sign_Board__c,AFZA_Charges__c, AFZA_Other_Charges__c,EMPost_Charges__c, Local_Government_Charges__c,FeeName__c,Price__c, License_Application_Type__c, License_App_Category__c, Id, Fee__c, Fee_Type__c From Service_Price_Book__c where License_Application_Type__c =: 'Amendment' AND License_App_Category__c =: amendmentObject.Account_License__c AND Fee_Type__c =: 'Activity2Industrial Fee' AND isActive__c =: true order by CreatedDate limit 1]);
                    
                    if(feeObject9.size() > 0)
                    {
                        tempInvoiceItem.License_Difference_Fee__c = feeObject9[0].Price__c;
                        tempInvoiceItem.Amount__c = tempInvoiceItem.Amount__c + tempInvoiceItem.License_Difference_Fee__c;
                        tempInvoiceItem.Amendment_Charges__c = tempInvoiceItem.Amount__c + tempInvoiceItem.License_Difference_Fee__c;
                        //tempInvoiceItem.AFZA_Charges__c = tempInvoiceItem.AFZA_Charges__c + tempInvoiceItem.License_Difference_Fee__c;
                        tempInvoiceItem.AFZA_Amendment_Charges__c = tempInvoiceItem.AFZA_Amendment_Charges__c + tempInvoiceItem.License_Difference_Fee__c;
                                                
                        /*//We only neeed to put the Invoice Line Item...
                        tempInvoiceItem = new Invoice_Line_Item__c();
                        //Mapping the Fields...
                        //tempInvoiceItem.Name = feeObject9[0].Name;
                        tempInvoiceItem.Name = feeObject9[0].FeeName__c;
                        //tempInvoiceItem.Fee__c = feeObject9[0].Id;
                        tempInvoiceItem.Fee__c = feeObject9[0].Fee__c;
                        tempInvoiceItem.Service_Price_Book__c = feeObject9[0].Id;
                        tempInvoiceItem.Fee_Type__c = feeObject9[0].Fee_Type__c;
                        tempInvoiceItem.Amount__c = feeObject9[0].Price__c;
                        
                        tempInvoiceItem.AFZA_Charges__c = feeObject9[0].AFZA_Charges__c;
                        tempInvoiceItem.AFZA_Other_Charges__c = feeObject9[0].AFZA_Other_Charges__c;
                        tempInvoiceItem.EMPost_Charges__c = feeObject9[0].EMPost_Charges__c;
                        tempInvoiceItem.Local_Government_Charges__c = feeObject9[0].Local_Government_Charges__c;
                        
                        tempInvoiceItem.Maintenance__c = feeObject9[0].Maintenance__c;
                        tempInvoiceItem.Establishment_Card__c = feeObject9[0].Establishment_Card__c;
                        tempInvoiceItem.Etisalat__c = feeObject9[0].Etisalat__c;
                        tempInvoiceItem.Google__c = feeObject9[0].Google__c;
                        tempInvoiceItem.Gov_Establishment_Card__c = feeObject9[0].Gov_Establishment_Card__c;
                        tempInvoiceItem.Sewage__c = feeObject9[0].Sewage__c;
                        tempInvoiceItem.Sign_Board__c = feeObject9[0].Sign_Board__c;
                        tempInvoiceItem.General_Services__c = feeObject9[0].General_Services__c;
                        
                        //Now Add the Invoice Item...
                        invoiceLineItemList.add(tempInvoiceItem);*/   
                        
                    }
                    
                }
                else if(amendmentObject.Account_License__c == 'Trading - Three Activity' && amendmentObject.New_License_Type__c == 'Trading - General Trading')
                {
                    List<Service_Price_Book__c> feeObject10 = new List<Service_Price_Book__c>([Select General_Services__c, Establishment_Card__c, Etisalat__c, Google__c, Gov_Establishment_Card__c, Maintenance__c, Sewage__c, Sign_Board__c,AFZA_Charges__c, AFZA_Other_Charges__c,EMPost_Charges__c, Local_Government_Charges__c,FeeName__c,Price__c, License_Application_Type__c, License_App_Category__c, Id, Fee__c, Fee_Type__c From Service_Price_Book__c where License_Application_Type__c =: 'Amendment' AND License_App_Category__c =: amendmentObject.Account_License__c AND Fee_Type__c =: 'Activity3General Fee' AND isActive__c =: true order by CreatedDate limit 1]);
                    
                    if(feeObject10.size() > 0)
                    {
                        tempInvoiceItem.License_Difference_Fee__c = feeObject10[0].Price__c;
                        tempInvoiceItem.Amount__c = tempInvoiceItem.Amount__c + tempInvoiceItem.License_Difference_Fee__c;
                        tempInvoiceItem.Amendment_Charges__c = tempInvoiceItem.Amount__c + tempInvoiceItem.License_Difference_Fee__c;
                        //tempInvoiceItem.AFZA_Charges__c = tempInvoiceItem.AFZA_Charges__c + tempInvoiceItem.License_Difference_Fee__c;
                        tempInvoiceItem.AFZA_Amendment_Charges__c = tempInvoiceItem.AFZA_Amendment_Charges__c + tempInvoiceItem.License_Difference_Fee__c;
                        
                        /*//We only neeed to put the Invoice Line Item...
                        tempInvoiceItem = new Invoice_Line_Item__c();
                        //Mapping the Fields...
                        //tempInvoiceItem.Name = feeObject10[0].Name;
                        tempInvoiceItem.Name = feeObject10[0].FeeName__c;
                        //tempInvoiceItem.Fee__c = feeObject10[0].Id;
                        tempInvoiceItem.Fee__c = feeObject10[0].Fee__c;
                        tempInvoiceItem.Service_Price_Book__c = feeObject10[0].Id;
                        tempInvoiceItem.Fee_Type__c = feeObject10[0].Fee_Type__c;
                        tempInvoiceItem.Amount__c = feeObject10[0].Price__c;
                        
                        tempInvoiceItem.AFZA_Charges__c = feeObject10[0].AFZA_Charges__c;
                        tempInvoiceItem.AFZA_Other_Charges__c = feeObject10[0].AFZA_Other_Charges__c;
                        tempInvoiceItem.EMPost_Charges__c = feeObject10[0].EMPost_Charges__c;
                        tempInvoiceItem.Local_Government_Charges__c = feeObject10[0].Local_Government_Charges__c;
                        
                        tempInvoiceItem.Maintenance__c = feeObject10[0].Maintenance__c;
                        tempInvoiceItem.Establishment_Card__c = feeObject10[0].Establishment_Card__c;
                        tempInvoiceItem.Etisalat__c = feeObject10[0].Etisalat__c;
                        tempInvoiceItem.Google__c = feeObject10[0].Google__c;
                        tempInvoiceItem.Gov_Establishment_Card__c = feeObject10[0].Gov_Establishment_Card__c;
                        tempInvoiceItem.Sewage__c = feeObject10[0].Sewage__c;
                        tempInvoiceItem.Sign_Board__c = feeObject10[0].Sign_Board__c;
                        tempInvoiceItem.General_Services__c = feeObject10[0].General_Services__c;
                            
                        //Now Add the Invoice Item...
                        invoiceLineItemList.add(tempInvoiceItem);*/   
                    }
                    
                }
                else if(amendmentObject.Account_License__c == 'Trading - Three Activity' && amendmentObject.New_License_Type__c == 'Services')
                {
                    List<Service_Price_Book__c> feeObject11 = new List<Service_Price_Book__c>([Select General_Services__c, Establishment_Card__c, Etisalat__c, Google__c, Gov_Establishment_Card__c, Maintenance__c, Sewage__c, Sign_Board__c,AFZA_Charges__c, AFZA_Other_Charges__c,EMPost_Charges__c, Local_Government_Charges__c,FeeName__c,Price__c, License_Application_Type__c, License_App_Category__c, Id, Fee__c, Fee_Type__c From Service_Price_Book__c where License_Application_Type__c =: 'Amendment' AND License_App_Category__c =: amendmentObject.Account_License__c AND Fee_Type__c =: 'Activity3Services Fee' AND isActive__c =: true order by CreatedDate limit 1]);
                    
                    if(feeObject11.size() > 0)
                    {
                        tempInvoiceItem.License_Difference_Fee__c = feeObject11[0].Price__c;
                        tempInvoiceItem.Amount__c = tempInvoiceItem.Amount__c + tempInvoiceItem.License_Difference_Fee__c;
                        tempInvoiceItem.Amendment_Charges__c = tempInvoiceItem.Amount__c + tempInvoiceItem.License_Difference_Fee__c;
                        //tempInvoiceItem.AFZA_Charges__c = tempInvoiceItem.AFZA_Charges__c + tempInvoiceItem.License_Difference_Fee__c;
                        tempInvoiceItem.AFZA_Amendment_Charges__c = tempInvoiceItem.AFZA_Amendment_Charges__c + tempInvoiceItem.License_Difference_Fee__c;
                        
                        /*//We only neeed to put the Invoice Line Item...
                        tempInvoiceItem = new Invoice_Line_Item__c();
                        //Mapping the Fields...
                        //tempInvoiceItem.Name = feeObject11[0].Name;
                        tempInvoiceItem.Name = feeObject11[0].FeeName__c;
                        //tempInvoiceItem.Fee__c = feeObject11[0].Id;
                        tempInvoiceItem.Fee__c = feeObject11[0].Fee__c;
                        tempInvoiceItem.Service_Price_Book__c = feeObject11[0].Id;
                        tempInvoiceItem.Fee_Type__c = feeObject11[0].Fee_Type__c;
                        tempInvoiceItem.Amount__c = feeObject11[0].Price__c;
                        
                        tempInvoiceItem.AFZA_Charges__c = feeObject11[0].AFZA_Charges__c;
                        tempInvoiceItem.AFZA_Other_Charges__c = feeObject11[0].AFZA_Other_Charges__c;
                        tempInvoiceItem.EMPost_Charges__c = feeObject11[0].EMPost_Charges__c;
                        tempInvoiceItem.Local_Government_Charges__c = feeObject11[0].Local_Government_Charges__c;
                        
                        tempInvoiceItem.Maintenance__c = feeObject11[0].Maintenance__c;
                        tempInvoiceItem.Establishment_Card__c = feeObject11[0].Establishment_Card__c;
                        tempInvoiceItem.Etisalat__c = feeObject11[0].Etisalat__c;
                        tempInvoiceItem.Google__c = feeObject11[0].Google__c;
                        tempInvoiceItem.Gov_Establishment_Card__c = feeObject11[0].Gov_Establishment_Card__c;
                        tempInvoiceItem.Sewage__c = feeObject11[0].Sewage__c;
                        tempInvoiceItem.Sign_Board__c = feeObject11[0].Sign_Board__c;
                        tempInvoiceItem.General_Services__c = feeObject11[0].General_Services__c;
                        
                        //Now Add the Invoice Item...
                        invoiceLineItemList.add(tempInvoiceItem);*/
                    }
                   
                }
                else if(amendmentObject.Account_License__c == 'Trading - Three Activity' && amendmentObject.New_License_Type__c == 'Industrial')
                {
                    List<Service_Price_Book__c> feeObject12 = new List<Service_Price_Book__c>([Select General_Services__c, Establishment_Card__c, Etisalat__c, Google__c, Gov_Establishment_Card__c, Maintenance__c, Sewage__c, Sign_Board__c,AFZA_Charges__c, AFZA_Other_Charges__c,EMPost_Charges__c, Local_Government_Charges__c,FeeName__c,Price__c, License_Application_Type__c, License_App_Category__c, Id, Fee__c, Fee_Type__c From Service_Price_Book__c where License_Application_Type__c =: 'Amendment' AND License_App_Category__c =: amendmentObject.Account_License__c AND Fee_Type__c =: 'Activity3Services Fee' AND isActive__c =: true order by CreatedDate limit 1]);
                    
                    if(feeObject12.size() > 0)
                    {
                        tempInvoiceItem.License_Difference_Fee__c = feeObject12[0].Price__c;
                        tempInvoiceItem.Amount__c = tempInvoiceItem.Amount__c + tempInvoiceItem.License_Difference_Fee__c;
                        tempInvoiceItem.Amendment_Charges__c = tempInvoiceItem.Amount__c + tempInvoiceItem.License_Difference_Fee__c;
                        //tempInvoiceItem.AFZA_Charges__c = tempInvoiceItem.AFZA_Charges__c + tempInvoiceItem.License_Difference_Fee__c;
                        tempInvoiceItem.AFZA_Amendment_Charges__c = tempInvoiceItem.AFZA_Amendment_Charges__c + tempInvoiceItem.License_Difference_Fee__c;
                        
                        /*//We only neeed to put the Invoice Line Item...
                        tempInvoiceItem = new Invoice_Line_Item__c();
                        //Mapping the Fields...
                        //tempInvoiceItem.Name = feeObject12[0].Name;
                        tempInvoiceItem.Name = feeObject12[0].FeeName__c;
                        //tempInvoiceItem.Fee__c = feeObject12[0].Id;
                        tempInvoiceItem.Fee__c = feeObject12[0].Fee__c;
                        tempInvoiceItem.Service_Price_Book__c = feeObject12[0].Id;
                        tempInvoiceItem.Fee_Type__c = feeObject12[0].Fee_Type__c;
                        tempInvoiceItem.Amount__c = feeObject12[0].Price__c;
                        
                        tempInvoiceItem.AFZA_Charges__c = feeObject12[0].AFZA_Charges__c;
                        tempInvoiceItem.AFZA_Other_Charges__c = feeObject12[0].AFZA_Other_Charges__c;
                        tempInvoiceItem.EMPost_Charges__c = feeObject12[0].EMPost_Charges__c;
                        tempInvoiceItem.Local_Government_Charges__c = feeObject12[0].Local_Government_Charges__c;
                        
                        tempInvoiceItem.Maintenance__c = feeObject12[0].Maintenance__c;
                        tempInvoiceItem.Establishment_Card__c = feeObject12[0].Establishment_Card__c;
                        tempInvoiceItem.Etisalat__c = feeObject12[0].Etisalat__c;
                        tempInvoiceItem.Google__c = feeObject12[0].Google__c;
                        tempInvoiceItem.Gov_Establishment_Card__c = feeObject12[0].Gov_Establishment_Card__c;
                        tempInvoiceItem.Sewage__c = feeObject12[0].Sewage__c;
                        tempInvoiceItem.Sign_Board__c = feeObject12[0].Sign_Board__c;
                        tempInvoiceItem.General_Services__c = feeObject12[0].General_Services__c;
                        
                        //Now Add the Invoice Item...
                        invoiceLineItemList.add(tempInvoiceItem);*/   
                    }
                    
                }
                
                //Now Add the Invoice Item...2/October/2012
                if(feeObject.size() > 0)
                    invoiceLineItemList.add(tempInvoiceItem);   
                
                
            }
            else if(recordTypeName == 'Shareholder Amendment - Add Partner' || recordTypeName == 'Shareholder Amendment - Change Share Percentage' || recordTypeName == 'Shareholder Amendment - Remove Partner' || recordTypeName == 'Shareholder Amendment - Replace Partner')
            {
                //List<Fee__c> feeObjectList =  new List<Fee__c>();
                List<Service_Price_Book__c> feeObjectList = new List<Service_Price_Book__c>();
                
                
                /*if(recordTypeName == 'Shareholder Amendment - Add Partner')                
                    feeObjectList = [Select Id,Name,Price__c,Fee_Type__c,CreatedDate from Fee__c where  Fee_Type__c=: 'Amendment - Add Partner Fee' OR Fee_Type__c =: 'Amendment - New Article of Association Price' order by CreatedDate];
                else if(recordTypeName == 'Shareholder Amendment - Change Share Percentage')
                    feeObjectList = [Select Id,Name,Price__c,Fee_Type__c,CreatedDate from Fee__c where  Fee_Type__c=: 'Amendment - Change Share Percentage Fee' OR Fee_Type__c =: 'Amendment - New Article of Association Price' order by CreatedDate];
                else if(recordTypeName == 'Shareholder Amendment - Remove Partner')
                    feeObjectList = [Select Id,Name,Price__c,Fee_Type__c,CreatedDate from Fee__c where  Fee_Type__c=: 'Amendment - Remove Partner Fee' OR Fee_Type__c =: 'Amendment - New Article of Association Price' order by CreatedDate];    
                else if(recordTypeName == 'Shareholder Amendment - Replace Partner')
                    feeObjectList = [Select Id,Name,Price__c,Fee_Type__c,CreatedDate from Fee__c where  Fee_Type__c=: 'Amendment - Replace Partner Fee' OR Fee_Type__c =: 'Amendment - New Article of Association Price' order by CreatedDate];*/    
                
                if(recordTypeName == 'Shareholder Amendment - Add Partner')                
                    feeObjectList = [Select Employment_ID_Card__c,AFZA_Employment_ID_Card__c,Advertisement_Fee__c,AFZA_Advertisement_Fee__c,AFZA_Establishment_Card__c,AFZA_Etisalat__c,AFZA_General_Services__c,AFZA_Google__c,AFZA_License_Activity_Fee__c,License_Activity_Fee__c,AFZA_Maintenance__c,AFZA_Sign_Board__c,AFZA_Sewage__c,General_Services__c, Establishment_Card__c, Etisalat__c, Google__c, Gov_Establishment_Card__c, Maintenance__c, Sewage__c, Sign_Board__c,AFZA_Charges__c, AFZA_Other_Charges__c,  EMPost_Charges__c, Local_Government_Charges__c,FeeName__c,Price__c, License_Application_Type__c, License_App_Category__c, Id, Fee__c, Fee_Type__c From Service_Price_Book__c where License_Application_Type__c =: 'Amendment' AND License_App_Category__c =: amendmentObject.Account_License__c AND (Fee_Type__c=: 'Amendment - Add Partner Fee' OR Fee_Type__c =: 'Amendment - New Article of Association Price') AND isActive__c =: true order by CreatedDate];
                else if(recordTypeName == 'Shareholder Amendment - Change Share Percentage')
                    feeObjectList = [Select Employment_ID_Card__c,AFZA_Employment_ID_Card__c,Advertisement_Fee__c,AFZA_Advertisement_Fee__c,AFZA_Establishment_Card__c,AFZA_Etisalat__c,AFZA_General_Services__c,AFZA_Google__c,AFZA_License_Activity_Fee__c,License_Activity_Fee__c,AFZA_Maintenance__c,AFZA_Sign_Board__c,AFZA_Sewage__c,General_Services__c, Establishment_Card__c, Etisalat__c, Google__c, Gov_Establishment_Card__c, Maintenance__c, Sewage__c, Sign_Board__c,AFZA_Charges__c, AFZA_Other_Charges__c,  EMPost_Charges__c, Local_Government_Charges__c,FeeName__c,Price__c, License_Application_Type__c, License_App_Category__c, Id, Fee__c, Fee_Type__c From Service_Price_Book__c where License_Application_Type__c =: 'Amendment' AND License_App_Category__c =: amendmentObject.Account_License__c AND (Fee_Type__c=: 'Amendment - Change Share Percentage Fee' OR Fee_Type__c =: 'Amendment - New Article of Association Price') AND isActive__c =: true order by CreatedDate];
                else if(recordTypeName == 'Shareholder Amendment - Remove Partner')
                    feeObjectList = [Select Employment_ID_Card__c,AFZA_Employment_ID_Card__c,Advertisement_Fee__c,AFZA_Advertisement_Fee__c,AFZA_Establishment_Card__c,AFZA_Etisalat__c,AFZA_General_Services__c,AFZA_Google__c,AFZA_License_Activity_Fee__c,License_Activity_Fee__c,AFZA_Maintenance__c,AFZA_Sign_Board__c,AFZA_Sewage__c,General_Services__c, Establishment_Card__c, Etisalat__c, Google__c, Gov_Establishment_Card__c, Maintenance__c, Sewage__c, Sign_Board__c,AFZA_Charges__c, AFZA_Other_Charges__c,  EMPost_Charges__c, Local_Government_Charges__c,FeeName__c,Price__c, License_Application_Type__c, License_App_Category__c, Id, Fee__c, Fee_Type__c From Service_Price_Book__c where License_Application_Type__c =: 'Amendment' AND License_App_Category__c =: amendmentObject.Account_License__c AND (Fee_Type__c=: 'Amendment - Remove Partner Fee' OR Fee_Type__c =: 'Amendment - New Article of Association Price') AND isActive__c =: true order by CreatedDate];    
                else if(recordTypeName == 'Shareholder Amendment - Replace Partner')
                    feeObjectList = [Select Employment_ID_Card__c,AFZA_Employment_ID_Card__c,Advertisement_Fee__c,AFZA_Advertisement_Fee__c,AFZA_Establishment_Card__c,AFZA_Etisalat__c,AFZA_General_Services__c,AFZA_Google__c,AFZA_License_Activity_Fee__c,License_Activity_Fee__c,AFZA_Maintenance__c,AFZA_Sign_Board__c,AFZA_Sewage__c,General_Services__c, Establishment_Card__c, Etisalat__c, Google__c, Gov_Establishment_Card__c, Maintenance__c, Sewage__c, Sign_Board__c,AFZA_Charges__c, AFZA_Other_Charges__c,  EMPost_Charges__c, Local_Government_Charges__c,FeeName__c,Price__c, License_Application_Type__c, License_App_Category__c, Id, Fee__c, Fee_Type__c From Service_Price_Book__c where License_Application_Type__c =: 'Amendment' AND License_App_Category__c =: amendmentObject.Account_License__c AND (Fee_Type__c=: 'Amendment - Replace Partner Fee' OR Fee_Type__c =: 'Amendment - New Article of Association Price') AND isActive__c =: true order by CreatedDate];
                
                
                System.debug('The Record Type Name => '+ recordTypeName + '  && The Fee Object List Size => '+ feeObjectList.size());
                
                //for(Fee__c feeObject : feeObjectList)
                for(Service_Price_Book__c  feeObject : feeObjectList)
                {
                    //We only neeed to put the Invoice Line Item...
                    System.debug('The Fee Type => '+ feeObject.Fee_Type__c + '  && The Fee Object List Size => '+ feeObjectList.size());
                    tempInvoiceItem = new Invoice_Line_Item__c();
                    //Mapping the Fields...
                    //tempInvoiceItem.Name = feeObject.Name;
                    tempInvoiceItem.Name = feeObject.FeeName__c;
                    //tempInvoiceItem.Fee__c = feeObject.Id;
                    tempInvoiceItem.Fee__c = feeObject.Fee__c;
                    tempInvoiceItem.Service_Price_Book__c = feeObject.Id;
                    tempInvoiceItem.Fee_Type__c = feeObject.Fee_Type__c;
                    
                    tempInvoiceItem.AFZA_Charges__c = feeObject.AFZA_Charges__c;
                    tempInvoiceItem.AFZA_Other_Charges__c = feeObject.AFZA_Other_Charges__c;
                    tempInvoiceItem.EMPost_Charges__c = feeObject.EMPost_Charges__c;
                    tempInvoiceItem.Local_Government_Charges__c = feeObject.Local_Government_Charges__c;
                    
                    tempInvoiceItem.Maintenance__c = feeObject.Maintenance__c;
                    tempInvoiceItem.Establishment_Card__c = feeObject.Establishment_Card__c;
                    tempInvoiceItem.Etisalat__c = feeObject.Etisalat__c;
                    tempInvoiceItem.Google__c = feeObject.Google__c;
                    tempInvoiceItem.Gov_Establishment_Card__c = feeObject.Gov_Establishment_Card__c;
                    tempInvoiceItem.Sewage__c = feeObject.Sewage__c;
                    tempInvoiceItem.Sign_Board__c = feeObject.Sign_Board__c;
                    tempInvoiceItem.General_Services__c = feeObject.General_Services__c;
                    
                    tempInvoiceItem.AFZA_Maintenance__c = feeObject.AFZA_Maintenance__c;
                    tempInvoiceItem.AFZA_Establishment_Card__c = feeObject.AFZA_Establishment_Card__c;
                    tempInvoiceItem.AFZA_Etisalat__c = feeObject.AFZA_Etisalat__c;
                    tempInvoiceItem.AFZA_Google__c = feeObject.AFZA_Google__c;
                    tempInvoiceItem.AFZA_Sewage__c = feeObject.AFZA_Sewage__c;
                    tempInvoiceItem.AFZA_Sign_Board__c = feeObject.AFZA_Sign_Board__c;
                    tempInvoiceItem.AFZA_General_Services__c = feeObject.AFZA_General_Services__c;
                    
                    tempInvoiceItem.Advertisement_Fee__c = feeObject.Advertisement_Fee__c;
                    tempInvoiceItem.AFZA_Advertisement_Fee__c = feeObject.AFZA_Advertisement_Fee__c;
                    tempInvoiceItem.License_Activity_Fee__c = feeObject.License_Activity_Fee__c;
                    tempInvoiceItem.AFZA_License_Activity_Fee__c = feeObject.AFZA_License_Activity_Fee__c;
                    tempInvoiceItem.Employment_ID_Card__c = feeObject.Employment_ID_Card__c;
                    tempInvoiceItem.AFZA_Employment_ID_Card__c = feeObject.AFZA_Employment_ID_Card__c;
                    
                    
                    if(feeObject.Fee_Type__c != 'New Article of Association Price')
                    {
                        tempInvoiceItem.Amount__c = feeObject.Price__c;
                        tempInvoiceItem.Amendment_Charges__c = feeObject.Price__c;
                        tempInvoiceItem.AFZA_Amendment_Charges__c = feeObject.Price__c;
                    }
                    else
                    {
                        if(amendmentObject.Total_Share_Holders__c > 1)
                        {
                            tempInvoiceItem.Amount__c = feeObject.Price__c * amendmentObject.Company_Capital__c;
                        
                            if(tempInvoiceItem.Amount__c > 20000)
                                tempInvoiceItem.Amount__c = 20000;
                            else if(tempInvoiceItem.Amount__c < 1000)   
                                tempInvoiceItem.Amount__c = 1000;
                            
                            
                            tempInvoiceItem.Amendment_Charges__c = tempInvoiceItem.Amount__c;
                            tempInvoiceItem.AFZA_Amendment_Charges__c = tempInvoiceItem.Amount__c;
                            
                            
                        }
                             
                    }
                    
                    
                    
                    
                    
                    //Now Add the Invoice Item...
                    invoiceLineItemList.add(tempInvoiceItem);
                
                }
                                
                    
            }
            else if(recordTypeName == 'Company Amendment - Change Company Manager') 
            {
                //Amendment – Change Company Manager
                List<Service_Price_Book__c> feeObject = new List<Service_Price_Book__c>([Select Employment_ID_Card__c,AFZA_Employment_ID_Card__c,Advertisement_Fee__c,AFZA_Advertisement_Fee__c,AFZA_Establishment_Card__c,AFZA_Etisalat__c,AFZA_General_Services__c,AFZA_Google__c,AFZA_License_Activity_Fee__c,License_Activity_Fee__c,AFZA_Maintenance__c,AFZA_Sign_Board__c,AFZA_Sewage__c,General_Services__c, Establishment_Card__c, Etisalat__c, Google__c, Gov_Establishment_Card__c, Maintenance__c, Sewage__c, Sign_Board__c,AFZA_Charges__c, AFZA_Other_Charges__c,  EMPost_Charges__c, Local_Government_Charges__c,FeeName__c,Price__c, License_Application_Type__c, License_App_Category__c, Id, Fee__c, Fee_Type__c From Service_Price_Book__c where License_Application_Type__c =: 'Amendment' AND License_App_Category__c =: amendmentObject.Account_License__c AND Fee_Type__c =: 'Amendment - Change Company Manager' AND isActive__c =: true order by CreatedDate limit 1]);
                    
                if(feeObject.size() > 0)
                {
                    //We only neeed to put the Invoice Line Item...
                    tempInvoiceItem = new Invoice_Line_Item__c();
                    //Mapping the Fields...
                    //tempInvoiceItem.Name = feeObject.Name;
                    tempInvoiceItem.Name = feeObject[0].FeeName__c;
                    //tempInvoiceItem.Fee__c = feeObject[0].Id;
                    tempInvoiceItem.Fee__c = feeObject[0].Fee__c;
                    tempInvoiceItem.Service_Price_Book__c = feeObject[0].Id;
                    tempInvoiceItem.Fee_Type__c = feeObject[0].Fee_Type__c;
                    tempInvoiceItem.Amount__c = feeObject[0].Price__c;
                    tempInvoiceItem.Amendment_Charges__c = feeObject[0].Price__c;
                    tempInvoiceItem.AFZA_Amendment_Charges__c = feeObject[0].Price__c;
                    tempInvoiceItem.AFZA_Charges__c = feeObject[0].AFZA_Charges__c;
                    tempInvoiceItem.AFZA_Other_Charges__c = feeObject[0].AFZA_Other_Charges__c;
                    tempInvoiceItem.EMPost_Charges__c = feeObject[0].EMPost_Charges__c;
                    tempInvoiceItem.Local_Government_Charges__c = feeObject[0].Local_Government_Charges__c;
                    
                    tempInvoiceItem.Maintenance__c = feeObject[0].Maintenance__c;
                    tempInvoiceItem.Establishment_Card__c = feeObject[0].Establishment_Card__c;
                    tempInvoiceItem.Etisalat__c = feeObject[0].Etisalat__c;
                    tempInvoiceItem.Google__c = feeObject[0].Google__c;
                    tempInvoiceItem.Gov_Establishment_Card__c = feeObject[0].Gov_Establishment_Card__c;
                    tempInvoiceItem.Sewage__c = feeObject[0].Sewage__c;
                    tempInvoiceItem.Sign_Board__c = feeObject[0].Sign_Board__c;
                    tempInvoiceItem.General_Services__c = feeObject[0].General_Services__c;
                    
                    tempInvoiceItem.AFZA_Maintenance__c = feeObject[0].AFZA_Maintenance__c;
                    tempInvoiceItem.AFZA_Establishment_Card__c = feeObject[0].AFZA_Establishment_Card__c;
                    tempInvoiceItem.AFZA_Etisalat__c = feeObject[0].AFZA_Etisalat__c;
                    tempInvoiceItem.AFZA_Google__c = feeObject[0].AFZA_Google__c;
                    tempInvoiceItem.AFZA_Sewage__c = feeObject[0].AFZA_Sewage__c;
                    tempInvoiceItem.AFZA_Sign_Board__c = feeObject[0].AFZA_Sign_Board__c;
                    tempInvoiceItem.AFZA_General_Services__c = feeObject[0].AFZA_General_Services__c;
                    
                    tempInvoiceItem.Advertisement_Fee__c = feeObject[0].Advertisement_Fee__c;
                    tempInvoiceItem.AFZA_Advertisement_Fee__c = feeObject[0].AFZA_Advertisement_Fee__c;
                    tempInvoiceItem.License_Activity_Fee__c = feeObject[0].License_Activity_Fee__c;
                    tempInvoiceItem.AFZA_License_Activity_Fee__c = feeObject[0].AFZA_License_Activity_Fee__c;
                    
                    tempInvoiceItem.Employment_ID_Card__c = feeObject[0].Employment_ID_Card__c;
                    tempInvoiceItem.AFZA_Employment_ID_Card__c = feeObject[0].AFZA_Employment_ID_Card__c;
                                        
                    //Now Add the Invoice Item...
                    invoiceLineItemList.add(tempInvoiceItem);   
                }
                
                
                
            }
            
            else if(recordTypeName == 'Location Amendment - Change Location')
            {
                //Change Location Flat Fee...
                List<Service_Price_Book__c> feeObject = new List<Service_Price_Book__c>([Select locationServiceFlag__c,Employment_ID_Card__c,AFZA_Employment_ID_Card__c,Renewal_Amendment_Fee__c,AFZA_Renewal_Amendment_Fee__c,Advertisement_Fee__c,AFZA_Advertisement_Fee__c,AFZA_Establishment_Card__c,AFZA_Etisalat__c,AFZA_General_Services__c,AFZA_Google__c,AFZA_License_Activity_Fee__c,License_Activity_Fee__c,AFZA_Maintenance__c,AFZA_Sign_Board__c,AFZA_Sewage__c,General_Services__c, Establishment_Card__c, Etisalat__c, Google__c, Gov_Establishment_Card__c, Maintenance__c, Sewage__c, Sign_Board__c,AFZA_Charges__c, AFZA_Other_Charges__c,   EMPost_Charges__c, Local_Government_Charges__c,FeeName__c,Price__c, License_Application_Type__c, License_App_Category__c, Id, Fee__c, Fee_Type__c From Service_Price_Book__c where License_Application_Type__c =: 'Amendment' AND License_App_Category__c =: amendmentObject.Account_License__c AND Fee_Type__c =: 'Amendment - Change Location' AND isActive__c =: true order by CreatedDate limit 1]);
                //List<Service_Price_Book__c> servicePriceBookLocationList = new List<Service_Price_Book__c>([Select Name, AFZA_Charges__c, AFZA_Other_Charges__c,EMPost_Charges__c, Local_Government_Charges__c,Location_Type__c,FeeName__c,Price__c, License_Application_Type__c,Fee_Type__c,License_App_Category__c, Id, Fee__r.Fee_ID__c, Fee__r.Fee_Type__c, Fee__r.Receipt_Group__c, Fee__r.Price__c, Fee__r.Name, Fee__c, Description__c From Service_Price_Book__c where  License_Application_Type__c =: 'Amendment' AND License_App_Category__c =: 'Location' AND Fee_Type__c !=: 'Guarantee']);   
                List<Service_Price_Book__c> servicePriceBookLocationList = new List<Service_Price_Book__c>([Select locationServiceFlag__c,Employment_ID_Card__c,AFZA_Employment_ID_Card__c,Renewal_Amendment_Fee__c,AFZA_Renewal_Amendment_Fee__c,Advertisement_Fee__c,AFZA_Advertisement_Fee__c,AFZA_Establishment_Card__c,AFZA_Etisalat__c,AFZA_General_Services__c,AFZA_Google__c,AFZA_License_Activity_Fee__c,License_Activity_Fee__c,AFZA_Maintenance__c,AFZA_Sign_Board__c,AFZA_Sewage__c,General_Services__c, Establishment_Card__c, Etisalat__c, Google__c, Gov_Establishment_Card__c, Maintenance__c, Sewage__c, Sign_Board__c,Name, AFZA_Charges__c, AFZA_Other_Charges__c,EMPost_Charges__c, Local_Government_Charges__c,Location_Type__c,FeeName__c,Price__c, License_Application_Type__c,Fee_Type__c,License_App_Category__c, Id, Fee__r.Fee_ID__c, Fee__r.Fee_Type__c, Fee__r.Receipt_Group__c, Fee__r.Price__c, Fee__r.Name, Fee__c, Description__c From Service_Price_Book__c where  License_Application_Type__c =: 'Amendment' AND License_App_Category__c =: 'Location' AND isActive__c =: true]);
                
                //For Package Selection Adding in the Map...
                if(paymentPlanList.size() > 0)
                {
                    for(Payment_Plan__c paymentPlanObject : paymentPlanList)
                    {
                        //Adding in the Map...
                        paymentPlanMap.put(paymentPlanObject.Location_Type__c,paymentPlanObject);
                    }
                }
                
                if(feeObject.size() > 0)
                {
                    //We only neeed to put the Invoice Line Item...
                    tempInvoiceItem = new Invoice_Line_Item__c();
                    //Mapping the Fields...
                    //tempInvoiceItem.Name = feeObject.Name;
                    tempInvoiceItem.Name = feeObject[0].FeeName__c;
                    //tempInvoiceItem.Fee__c = feeObject[0].Id;
                    tempInvoiceItem.Fee__c = feeObject[0].Fee__c;
                    tempInvoiceItem.Service_Price_Book__c = feeObject[0].Id;
                    tempInvoiceItem.Fee_Type__c = feeObject[0].Fee_Type__c;
                    tempInvoiceItem.Amount__c = feeObject[0].Price__c;
                    tempInvoiceItem.Amendment_Charges__c = feeObject[0].Price__c;
                    tempInvoiceItem.AFZA_Amendment_Charges__c = feeObject[0].Price__c;
                    
                    tempInvoiceItem.AFZA_Charges__c = feeObject[0].AFZA_Charges__c;
                    tempInvoiceItem.AFZA_Other_Charges__c = feeObject[0].AFZA_Other_Charges__c;
                    tempInvoiceItem.EMPost_Charges__c = feeObject[0].EMPost_Charges__c;
                    tempInvoiceItem.Local_Government_Charges__c = feeObject[0].Local_Government_Charges__c;
                    
                    tempInvoiceItem.Maintenance__c = feeObject[0].Maintenance__c;
                    tempInvoiceItem.Establishment_Card__c = feeObject[0].Establishment_Card__c;
                    tempInvoiceItem.Etisalat__c = feeObject[0].Etisalat__c;
                    tempInvoiceItem.Google__c = feeObject[0].Google__c;
                    tempInvoiceItem.Gov_Establishment_Card__c = feeObject[0].Gov_Establishment_Card__c;
                    tempInvoiceItem.Sewage__c = feeObject[0].Sewage__c;
                    tempInvoiceItem.Sign_Board__c = feeObject[0].Sign_Board__c;
                    tempInvoiceItem.General_Services__c = feeObject[0].General_Services__c;
                    
                    tempInvoiceItem.AFZA_Maintenance__c = feeObject[0].AFZA_Maintenance__c;
                    tempInvoiceItem.AFZA_Establishment_Card__c = feeObject[0].AFZA_Establishment_Card__c;
                    tempInvoiceItem.AFZA_Etisalat__c = feeObject[0].AFZA_Etisalat__c;
                    tempInvoiceItem.AFZA_Google__c = feeObject[0].AFZA_Google__c;
                    tempInvoiceItem.AFZA_Sewage__c = feeObject[0].AFZA_Sewage__c;
                    tempInvoiceItem.AFZA_Sign_Board__c = feeObject[0].AFZA_Sign_Board__c;
                    tempInvoiceItem.AFZA_General_Services__c = feeObject[0].AFZA_General_Services__c;
                    
                    tempInvoiceItem.Advertisement_Fee__c = feeObject[0].Advertisement_Fee__c;
                    tempInvoiceItem.AFZA_Advertisement_Fee__c = feeObject[0].AFZA_Advertisement_Fee__c;
                    tempInvoiceItem.License_Activity_Fee__c = feeObject[0].License_Activity_Fee__c;
                    tempInvoiceItem.AFZA_License_Activity_Fee__c = feeObject[0].AFZA_License_Activity_Fee__c;
                                        
                    tempInvoiceItem.Employment_ID_Card__c = feeObject[0].Employment_ID_Card__c;
                    tempInvoiceItem.AFZA_Employment_ID_Card__c = feeObject[0].AFZA_Employment_ID_Card__c;
                    
                    //Now Add the Invoice Item...
                    invoiceLineItemList.add(tempInvoiceItem);
                    
                    
                }
                
                //For tenancy Cotract Invoice Itemm...
                  
                //We only neeed to put the Invoice Line Item...
                tempInvoiceItem = new Invoice_Line_Item__c();   
                //Mapping the Fields...
                //tempInvoiceItem.Name = 'Tenancy Contract Fee : ' + amendmentObject.New_Location_1_Name__c;
                //tempInvoiceItem.Name = 'Tenancy Contract Fee';
                tempInvoiceItem.Name = 'Location Rental Rate';
                tempInvoiceItem.Location__c = amendmentObject.New_Location__c;
                System.debug('The amendmentObject.New_Location_1_Name__c => '+ amendmentObject.New_Location_1_Name__c +' && The tempInvoiceItem.name ==> '+ tempInvoiceItem.Name);
                
                System.debug('The NLPPD Price ==> '+ amendmentObject.NLPPD__c );
                System.debug('The NTCDI Price ==> '+ amendmentObject.NTCDI__c);
                System.debug('The PTCPPD Price ==> '+ amendmentObject.PTCPPD__c);
                System.debug('The OTCDL Price ==> '+ amendmentObject.OTCDL__c);
                
                decimal tenancyPrice = ((amendmentObject.NLPPD__c * amendmentObject.NTCDI__c) - (amendmentObject.PTCPPD__c * amendmentObject.OTCDL__c)).setScale(2);
                System.debug('The Tenancy Price ==> '+ tenancyPrice);
                
                if(tenancyPrice < 0)
                    tenancyPrice = 0;
                    
                tempInvoiceItem.Start_Date__c = System.today();
                tempInvoiceItem.End_Date__c = amendmentObject.Account__r.License_Expiry_Date__c;
                    
                tempInvoiceItem.Amount__c = tenancyPrice;
                tempInvoiceItem.Location_Rental_Rate__c = tenancyPrice;
                tempInvoiceItem.AFZA_Location_Rental_Rate__c = tenancyPrice;
                
                
                //Package Selection...
                if(paymentPlanMap.get(amendmentObject.New_Location_Type_1__c) != null)
                {
                    tempInvoiceItem.Name = 'Location Rental Rate ' + paymentPlanMap.get(amendmentObject.New_Location_Type_1__c).Installment__c + ' Installments'; 
                    tempInvoiceItem.Location_Rental_Rate__c = tenancyPrice + (tenancyPrice * paymentPlanMap.get(amendmentObject.New_Location_Type_1__c).Sign_Multiplier__c * (paymentPlanMap.get(amendmentObject.New_Location_Type_1__c).Percent__c / 100)    );
                    tempInvoiceItem.AFZA_Location_Rental_Rate__c = tenancyPrice + (tenancyPrice * paymentPlanMap.get(amendmentObject.New_Location_Type_1__c).Sign_Multiplier__c * (paymentPlanMap.get(amendmentObject.New_Location_Type_1__c).Percent__c / 100)    );
                    tempInvoiceItem.Payment_Plan__c = paymentPlanMap.get(amendmentObject.New_Location_Type_1__c).Id;
                    tempInvoiceItem.Package_Value__c = tenancyPrice * paymentPlanMap.get(amendmentObject.New_Location_Type_1__c).Sign_Multiplier__c * (paymentPlanMap.get(amendmentObject.New_Location_Type_1__c).Percent__c / 100);
                    tempInvoiceItem.Installment__c = paymentPlanMap.get(amendmentObject.New_Location_Type_1__c).Installment__c;
                    tempInvoiceItem.Action__c = paymentPlanMap.get(amendmentObject.New_Location_Type_1__c).Action__c;
                    //The Amount...
                    tempInvoiceItem.Amount__c = (tenancyPrice + (tenancyPrice * paymentPlanMap.get(amendmentObject.New_Location_Type_1__c).Sign_Multiplier__c * (paymentPlanMap.get(amendmentObject.New_Location_Type_1__c).Percent__c / 100)    )).setScale(2);
                }
                
                
                
                //Now Add the Invoice Item...
                invoiceLineItemList.add(tempInvoiceItem);
                
                //Also doing for the location... 
                if(servicePriceBookLocationList.size() > 0)
                {
                    //For the Service Price Book....
                    for(Service_Price_Book__c tempServiceObject : servicePriceBookLocationList)
                    {
                        //Office, Warehouse, Accomodation,Land, E.O.C, Shop...
                        if(tempServiceObject.Location_Type__c == amendmentObject.New_Location_Type_1__c)        
                        {
                            if(tempServiceObject.Fee_Type__c == 'Guarantee')
                                guaranteeInvoiceFlag = true;
                                    
                            //Now Create a Invoice Line Item for this...
                            tempInvoiceItem = new Invoice_Line_Item__c();
                            //Mapping the Fields....
                            //tempInvoiceItem.Name = bookingObject.Location__r.Name + ' - '+ tempServiceObject.Location_Type__c +' Rental Rate';
                            //tempInvoiceItem.Name = amendmentObject.New_Location_1_Name__c + ' - '+ tempServiceObject.FeeName__c +' Rental Rate';
                            //tempInvoiceItem.Name = tempServiceObject.FeeName__c +' Rental Rate';
                            tempInvoiceItem.Name = tempServiceObject.FeeName__c;
                            tempInvoiceItem.Fee__c = tempServiceObject.Fee__c;
                            tempInvoiceItem.Location__c = amendmentObject.New_Location__c;
                            tempInvoiceItem.Service_Price_Book__c = tempServiceObject.Id;
                            tempInvoiceItem.Fee_Type__c = tempServiceObject.Fee_Type__c;
                            //tempInvoiceItem.Amount__c = tempServiceObject.Price__c;
                            if(tempServiceObject.locationServiceFlag__c == 'False')
                                tempInvoiceItem.Amount__c = tempServiceObject.Price__c;
                            else
                            {
                                Decimal daybetweenValue = Decimal.valueof((System.today()).daysbetween(amendmentObject.Account__r.License_Expiry_Date__c)).setScale(2);
                                tempInvoiceItem.Amount__c = ((tempServiceObject.Price__c / 360) * (daybetweenValue)).round(system.roundingMode.CEILING);
                                
                                tempInvoiceItem.Start_Date__c = System.today();
                                tempInvoiceItem.End_Date__c = amendmentObject.Account__r.License_Expiry_Date__c;
                                
                            }
                            
                            tempInvoiceItem.AFZA_Charges__c = tempServiceObject.AFZA_Charges__c;
                            tempInvoiceItem.AFZA_Other_Charges__c = tempServiceObject.AFZA_Other_Charges__c;
                            tempInvoiceItem.EMPost_Charges__c = tempServiceObject.EMPost_Charges__c;
                            tempInvoiceItem.Local_Government_Charges__c = tempServiceObject.Local_Government_Charges__c;
                            
                            tempInvoiceItem.Maintenance__c = tempServiceObject.Maintenance__c;
                            tempInvoiceItem.Establishment_Card__c = tempServiceObject.Establishment_Card__c;
                            tempInvoiceItem.Etisalat__c = tempServiceObject.Etisalat__c;
                            tempInvoiceItem.Google__c = tempServiceObject.Google__c;
                            tempInvoiceItem.Gov_Establishment_Card__c = tempServiceObject.Gov_Establishment_Card__c;
                            tempInvoiceItem.Sewage__c = tempServiceObject.Sewage__c;
                            tempInvoiceItem.Sign_Board__c = tempServiceObject.Sign_Board__c;
                            tempInvoiceItem.General_Services__c = tempServiceObject.General_Services__c;
                            
                            tempInvoiceItem.AFZA_Maintenance__c = tempServiceObject.AFZA_Maintenance__c;
                            tempInvoiceItem.AFZA_Establishment_Card__c = tempServiceObject.AFZA_Establishment_Card__c;
                            tempInvoiceItem.AFZA_Etisalat__c = tempServiceObject.AFZA_Etisalat__c;
                            tempInvoiceItem.AFZA_Google__c = tempServiceObject.AFZA_Google__c;
                            tempInvoiceItem.AFZA_Sewage__c = tempServiceObject.AFZA_Sewage__c;
                            tempInvoiceItem.AFZA_Sign_Board__c = tempServiceObject.AFZA_Sign_Board__c;
                            tempInvoiceItem.AFZA_General_Services__c = tempServiceObject.AFZA_General_Services__c;
                            
                            tempInvoiceItem.Advertisement_Fee__c = tempServiceObject.Advertisement_Fee__c;
                            tempInvoiceItem.AFZA_Advertisement_Fee__c = tempServiceObject.AFZA_Advertisement_Fee__c;
                            tempInvoiceItem.License_Activity_Fee__c = tempServiceObject.License_Activity_Fee__c;
                            tempInvoiceItem.AFZA_License_Activity_Fee__c = tempServiceObject.AFZA_License_Activity_Fee__c;
                            
                            tempInvoiceItem.Employment_ID_Card__c = tempServiceObject.Employment_ID_Card__c;
                            tempInvoiceItem.AFZA_Employment_ID_Card__c = tempServiceObject.AFZA_Employment_ID_Card__c;
                            
                            //Now Add the Invoice Item in List...
                            invoiceLineItemList.add(tempInvoiceItem);
                            
                        }
                    }
                }
                
                
            }
            
            else if(recordTypeName == 'Company Amendment - Change Company Name')
            {
                //Change Location Flat Fee...
                //List<Fee__c> feeObjectList = new List<Fee__c>([Select Id,Name,Price__c,Fee_Type__c,CreatedDate from Fee__c where  Fee_Type__c =: 'Amendment - Change Company Name' OR Fee_Type__c =:'Advertisement Fee' OR Fee_Type__c =:'Establishment Card Fee' OR Fee_Type__c =:'Employment ID Card Fee' ]);
                List<Service_Price_Book__c> feeObjectList = new List<Service_Price_Book__c>([Select Employment_ID_Card__c,AFZA_Employment_ID_Card__c,Advertisement_Fee__c,AFZA_Advertisement_Fee__c,AFZA_Establishment_Card__c,AFZA_Etisalat__c,AFZA_General_Services__c,AFZA_Google__c,AFZA_License_Activity_Fee__c,License_Activity_Fee__c,AFZA_Maintenance__c,AFZA_Sign_Board__c,AFZA_Sewage__c,General_Services__c, Establishment_Card__c, Etisalat__c, Google__c, Gov_Establishment_Card__c, Maintenance__c, Sewage__c, Sign_Board__c,AFZA_Charges__c, AFZA_Other_Charges__c,  EMPost_Charges__c, Local_Government_Charges__c,FeeName__c,Price__c, License_Application_Type__c, License_App_Category__c, Id, Fee__c, Fee_Type__c From Service_Price_Book__c where License_Application_Type__c =: 'Amendment' AND License_App_Category__c =: amendmentObject.Account_License__c AND ( Fee_Type__c =: 'Amendment - Change Company Name' OR Fee_Type__c =:'Advertisement Fee' OR Fee_Type__c =:'Establishment Card Fee' OR Fee_Type__c =:'Employment ID Card Fee') AND isActive__c =: true ]);
                
                //List<Contact> contactObjectList = new List<Contact>([select Id,Visa_Validity_Status__c,AccountId FROM Contact where AccountId =: accountId AND Visa_Validity_Status__c != 'Cancelled']);
                //List<Contact> contactObjectList = new List<Contact>([select Id,Visa_Validity_Status__c,Current_Employer__c,AccountId FROM Contact where Current_Employer__c =: accountId AND Visa_Validity_Status__c !=: 'Cancelled']);
                List<Contact> contactObjectList = new List<Contact>([select Id,Visa_Validity_Status__c,Current_Employer__c,AccountId FROM Contact where Current_Employer__c =: accountId AND Visa_Validity_Status__c =: 'Valid']);
                //for(Fee__c feeObject : feeObjectList)
                for(Service_Price_Book__c feeObject : feeObjectList)
                {    
                    tempInvoiceItem = new Invoice_Line_Item__c();
                    //Mapping the Fields...
                    //tempInvoiceItem.Name = feeObject.Name;
                    tempInvoiceItem.Name = feeObject.FeeName__c;
                    //tempInvoiceItem.Fee__c = feeObject.Id;
                    tempInvoiceItem.Fee__c = feeObject.Fee__c;
                    tempInvoiceItem.Service_Price_Book__c = feeObject.Id;
                    tempInvoiceItem.Fee_Type__c = feeObject.Fee_Type__c;
                    tempInvoiceItem.Amount__c = feeObject.Price__c;
                    
                    tempInvoiceItem.AFZA_Charges__c = feeObject.AFZA_Charges__c;
                    tempInvoiceItem.AFZA_Other_Charges__c = feeObject.AFZA_Other_Charges__c;
                    tempInvoiceItem.EMPost_Charges__c = feeObject.EMPost_Charges__c;
                    tempInvoiceItem.Local_Government_Charges__c = feeObject.Local_Government_Charges__c;
                    
                    tempInvoiceItem.Maintenance__c = feeObject.Maintenance__c;
                    tempInvoiceItem.Establishment_Card__c = feeObject.Establishment_Card__c;
                    tempInvoiceItem.Etisalat__c = feeObject.Etisalat__c;
                    tempInvoiceItem.Google__c = feeObject.Google__c;
                    tempInvoiceItem.Gov_Establishment_Card__c = feeObject.Gov_Establishment_Card__c;
                    tempInvoiceItem.Sewage__c = feeObject.Sewage__c;
                    tempInvoiceItem.Sign_Board__c = feeObject.Sign_Board__c;
                    tempInvoiceItem.General_Services__c = feeObject.General_Services__c;
                    
                    tempInvoiceItem.AFZA_Maintenance__c = feeObject.AFZA_Maintenance__c;
                    tempInvoiceItem.AFZA_Establishment_Card__c = feeObject.AFZA_Establishment_Card__c;
                    tempInvoiceItem.AFZA_Etisalat__c = feeObject.AFZA_Etisalat__c;
                    tempInvoiceItem.AFZA_Google__c = feeObject.AFZA_Google__c;
                    tempInvoiceItem.AFZA_Sewage__c = feeObject.AFZA_Sewage__c;
                    tempInvoiceItem.AFZA_Sign_Board__c = feeObject.AFZA_Sign_Board__c;
                    tempInvoiceItem.AFZA_General_Services__c = feeObject.AFZA_General_Services__c;
                    
                    tempInvoiceItem.Advertisement_Fee__c = feeObject.Advertisement_Fee__c;
                    tempInvoiceItem.AFZA_Advertisement_Fee__c = feeObject.AFZA_Advertisement_Fee__c;
                    tempInvoiceItem.License_Activity_Fee__c = feeObject.License_Activity_Fee__c;
                    tempInvoiceItem.AFZA_License_Activity_Fee__c = feeObject.AFZA_License_Activity_Fee__c;
                    
                    tempInvoiceItem.Employment_ID_Card__c = feeObject.Employment_ID_Card__c;
                    tempInvoiceItem.AFZA_Employment_ID_Card__c = feeObject.AFZA_Employment_ID_Card__c;
                    
                    if(contactObjectList.size() > 0)
                        tempInvoiceItem.Amount__c = (feeObject.Price__c - feeObject.Employment_ID_Card__c) + (feeObject.Employment_ID_Card__c * contactObjectList.size());
                  
                    //Previous Approach...(4th October 2012)  
                    /*if(feeObject.Fee_Type__c != 'Employment ID Card Fee')
                        tempInvoiceItem.Amount__c = feeObject.Price__c;
                    else
                        tempInvoiceItem.Amount__c = feeObject.Price__c * contactObjectList.size();
                    */
                    
                    
                    tempInvoiceItem.Amendment_Charges__c = tempInvoiceItem.Amount__c;
                    tempInvoiceItem.AFZA_Amendment_Charges__c = tempInvoiceItem.Amount__c;
                    
                    //Now Add the Invoice Item...
                    invoiceLineItemList.add(tempInvoiceItem);
                }
                
                /*if(accountlegalStatus == 'Free Zone Entity - F.Z.E')
                {
                    List<Service_Price_Book__c> feeObject = new List<Service_Price_Book__c>([Select General_Services__c, Establishment_Card__c, Etisalat__c, Google__c, Gov_Establishment_Card__c, Maintenance__c, Sewage__c, Sign_Board__c,AFZA_Charges__c, AFZA_Other_Charges__c, EMPost_Charges__c, Local_Government_Charges__c,FeeName__c,Price__c, License_Application_Type__c, License_App_Category__c, Id, Fee__c, Fee_Type__c From Service_Price_Book__c where License_Application_Type__c =: 'Amendment' AND License_App_Category__c =: amendmentObject.Account_License__c AND Fee_Type__c =: 'Free Zone Entity Fee' AND isActive__c =: true order by CreatedDate limit 1]);
                    
                    if(feeObject.size() > 0)
                    {
                        //We only neeed to put the Invoice Line Item...
                        tempInvoiceItem = new Invoice_Line_Item__c();
                        //Mapping the Fields...
                        //tempInvoiceItem.Name = feeObject.Name;
                        tempInvoiceItem.Name = feeObject[0].FeeName__c;
                        //tempInvoiceItem.Fee__c = feeObject[0].Id;
                        tempInvoiceItem.Fee__c = feeObject[0].Fee__c;
                        tempInvoiceItem.Service_Price_Book__c = feeObject[0].Id;
                        tempInvoiceItem.Fee_Type__c = feeObject[0].Fee_Type__c;
                        tempInvoiceItem.Amount__c = feeObject[0].Price__c;
                        
                        tempInvoiceItem.AFZA_Charges__c = feeObject[0].AFZA_Charges__c;
                        tempInvoiceItem.AFZA_Other_Charges__c = feeObject[0].AFZA_Other_Charges__c;
                        tempInvoiceItem.EMPost_Charges__c = feeObject[0].EMPost_Charges__c;
                        tempInvoiceItem.Local_Government_Charges__c = feeObject[0].Local_Government_Charges__c;
                        
                        tempInvoiceItem.Maintenance__c = feeObject[0].Maintenance__c;
                        tempInvoiceItem.Establishment_Card__c = feeObject[0].Establishment_Card__c;
                        tempInvoiceItem.Etisalat__c = feeObject[0].Etisalat__c;
                        tempInvoiceItem.Google__c = feeObject[0].Google__c;
                        tempInvoiceItem.Gov_Establishment_Card__c = feeObject[0].Gov_Establishment_Card__c;
                        tempInvoiceItem.Sewage__c = feeObject[0].Sewage__c;
                        tempInvoiceItem.Sign_Board__c = feeObject[0].Sign_Board__c;
                        tempInvoiceItem.General_Services__c = feeObject[0].General_Services__c;
                        
                        //Now Add the Invoice Item...
                        invoiceLineItemList.add(tempInvoiceItem);
                    }
                    
                
                }  */  
                
              
                
            }
            
            else if(recordTypeName == 'Company Amendment - Change Company Activity')
            {
                List<Service_Price_Book__c> feeObject = new List<Service_Price_Book__c>([Select Advertisement_Fee__c,AFZA_Advertisement_Fee__c,AFZA_Establishment_Card__c,AFZA_Etisalat__c,AFZA_General_Services__c,AFZA_Google__c,AFZA_License_Activity_Fee__c,License_Activity_Fee__c,AFZA_Maintenance__c,AFZA_Sign_Board__c,AFZA_Sewage__c,Employment_ID_Card__c,AFZA_Employment_ID_Card__c,General_Services__c, Establishment_Card__c, Etisalat__c, Google__c, Gov_Establishment_Card__c, Maintenance__c, Sewage__c, Sign_Board__c,AFZA_Charges__c, AFZA_Other_Charges__c,  EMPost_Charges__c, Local_Government_Charges__c,FeeName__c,Price__c, License_Application_Type__c, License_App_Category__c, Id, Fee__c, Fee_Type__c From Service_Price_Book__c where License_Application_Type__c =: 'Amendment' AND License_App_Category__c =: amendmentObject.Account_License__c AND Fee_Type__c =: 'Amendment - Change Activity' AND isActive__c =: true order by CreatedDate limit 1]);
                    
                if(feeObject.size() > 0)
                {
                    //We only neeed to put the Invoice Line Item...
                    tempInvoiceItem = new Invoice_Line_Item__c();
                    //Mapping the Fields...
                    //tempInvoiceItem.Name = feeObject[0].Name;
                    tempInvoiceItem.Name = feeObject[0].FeeName__c;
                    //tempInvoiceItem.Fee__c = feeObject[0].Id;
                    tempInvoiceItem.Fee__c = feeObject[0].Fee__c;
                    tempInvoiceItem.Service_Price_Book__c = feeObject[0].Id;
                    tempInvoiceItem.Fee_Type__c = feeObject[0].Fee_Type__c;
                    tempInvoiceItem.Amount__c = feeObject[0].Price__c;
                    tempInvoiceItem.Amendment_Charges__c = feeObject[0].Price__c;
                    tempInvoiceItem.AFZA_Amendment_Charges__c = feeObject[0].Price__c;
                    
                    tempInvoiceItem.AFZA_Charges__c = feeObject[0].AFZA_Charges__c;
                    tempInvoiceItem.AFZA_Other_Charges__c = feeObject[0].AFZA_Other_Charges__c;
                    tempInvoiceItem.EMPost_Charges__c = feeObject[0].EMPost_Charges__c;
                    tempInvoiceItem.Local_Government_Charges__c = feeObject[0].Local_Government_Charges__c;
                    
                    tempInvoiceItem.Maintenance__c = feeObject[0].Maintenance__c;
                    tempInvoiceItem.Establishment_Card__c = feeObject[0].Establishment_Card__c;
                    tempInvoiceItem.Etisalat__c = feeObject[0].Etisalat__c;
                    tempInvoiceItem.Google__c = feeObject[0].Google__c;
                    tempInvoiceItem.Gov_Establishment_Card__c = feeObject[0].Gov_Establishment_Card__c;
                    tempInvoiceItem.Sewage__c = feeObject[0].Sewage__c;
                    tempInvoiceItem.Sign_Board__c = feeObject[0].Sign_Board__c;
                    tempInvoiceItem.General_Services__c = feeObject[0].General_Services__c;
                    
                    tempInvoiceItem.AFZA_Maintenance__c = feeObject[0].AFZA_Maintenance__c;
                    tempInvoiceItem.AFZA_Establishment_Card__c = feeObject[0].AFZA_Establishment_Card__c;
                    tempInvoiceItem.AFZA_Etisalat__c = feeObject[0].AFZA_Etisalat__c;
                    tempInvoiceItem.AFZA_Google__c = feeObject[0].AFZA_Google__c;
                    tempInvoiceItem.AFZA_Sewage__c = feeObject[0].AFZA_Sewage__c;
                    tempInvoiceItem.AFZA_Sign_Board__c = feeObject[0].AFZA_Sign_Board__c;
                    tempInvoiceItem.AFZA_General_Services__c = feeObject[0].AFZA_General_Services__c;
                    
                    tempInvoiceItem.Advertisement_Fee__c = feeObject[0].Advertisement_Fee__c;
                    tempInvoiceItem.AFZA_Advertisement_Fee__c = feeObject[0].AFZA_Advertisement_Fee__c;
                    tempInvoiceItem.License_Activity_Fee__c = feeObject[0].License_Activity_Fee__c;
                    tempInvoiceItem.AFZA_License_Activity_Fee__c = feeObject[0].AFZA_License_Activity_Fee__c;
                    
                    
                    tempInvoiceItem.Employment_ID_Card__c = feeObject[0].Employment_ID_Card__c;
                    tempInvoiceItem.AFZA_Employment_ID_Card__c = feeObject[0].AFZA_Employment_ID_Card__c;
                    
                    //Now Add the Invoice Item...
                    invoiceLineItemList.add(tempInvoiceItem);   
                }
                
                
                
            }
            //For Amendment New Key Line Items...
            else if(recordTypeName == 'Location Amendment - Remove Location')
            {
                
                List<Service_Price_Book__c> feeObject = new List<Service_Price_Book__c>([Select Advertisement_Fee__c,AFZA_Advertisement_Fee__c,AFZA_Establishment_Card__c,AFZA_Etisalat__c,AFZA_General_Services__c,AFZA_Google__c,AFZA_License_Activity_Fee__c,License_Activity_Fee__c,AFZA_Maintenance__c,AFZA_Sign_Board__c,AFZA_Sewage__c,Employment_ID_Card__c,AFZA_Employment_ID_Card__c,General_Services__c, Establishment_Card__c, Etisalat__c, Google__c, Gov_Establishment_Card__c, Maintenance__c, Sewage__c, Sign_Board__c,AFZA_Charges__c, AFZA_Other_Charges__c,  EMPost_Charges__c, Local_Government_Charges__c,FeeName__c,Price__c, License_Application_Type__c, License_App_Category__c, Id, Fee__c, Fee_Type__c From Service_Price_Book__c where License_Application_Type__c =: 'Amendment' AND License_App_Category__c =: amendmentObject.Account_License__c AND Fee_Type__c =: 'Amendment - Remove Location' AND isActive__c =: true order by CreatedDate limit 1]);
                    
                if(feeObject.size() > 0)
                {
                    //We only neeed to put the Invoice Line Item...
                    tempInvoiceItem = new Invoice_Line_Item__c();
                    
                    //Mapping the Fields...
                    //tempInvoiceItem.Name = feeObject[0].Name;
                    tempInvoiceItem.Name = feeObject[0].FeeName__c;
                    //tempInvoiceItem.Fee__c = feeObject[0].Id;
                    tempInvoiceItem.Fee__c = feeObject[0].Fee__c;
                    tempInvoiceItem.Service_Price_Book__c = feeObject[0].Id;
                    tempInvoiceItem.Fee_Type__c = feeObject[0].Fee_Type__c;
                    tempInvoiceItem.Amount__c = feeObject[0].Price__c;
                    tempInvoiceItem.Amendment_Charges__c = feeObject[0].Price__c;
                    tempInvoiceItem.AFZA_Amendment_Charges__c = feeObject[0].Price__c;
                    
                    tempInvoiceItem.AFZA_Charges__c = feeObject[0].AFZA_Charges__c;
                    tempInvoiceItem.AFZA_Other_Charges__c = feeObject[0].AFZA_Other_Charges__c;
                    tempInvoiceItem.EMPost_Charges__c = feeObject[0].EMPost_Charges__c;
                    tempInvoiceItem.Local_Government_Charges__c = feeObject[0].Local_Government_Charges__c;
                    
                    tempInvoiceItem.Maintenance__c = feeObject[0].Maintenance__c;
                    tempInvoiceItem.Establishment_Card__c = feeObject[0].Establishment_Card__c;
                    tempInvoiceItem.Etisalat__c = feeObject[0].Etisalat__c;
                    tempInvoiceItem.Google__c = feeObject[0].Google__c;
                    tempInvoiceItem.Gov_Establishment_Card__c = feeObject[0].Gov_Establishment_Card__c;
                    tempInvoiceItem.Sewage__c = feeObject[0].Sewage__c;
                    tempInvoiceItem.Sign_Board__c = feeObject[0].Sign_Board__c;
                    tempInvoiceItem.General_Services__c = feeObject[0].General_Services__c;
                    
                    tempInvoiceItem.AFZA_Maintenance__c = feeObject[0].AFZA_Maintenance__c;
                    tempInvoiceItem.AFZA_Establishment_Card__c = feeObject[0].AFZA_Establishment_Card__c;
                    tempInvoiceItem.AFZA_Etisalat__c = feeObject[0].AFZA_Etisalat__c;
                    tempInvoiceItem.AFZA_Google__c = feeObject[0].AFZA_Google__c;
                    tempInvoiceItem.AFZA_Sewage__c = feeObject[0].AFZA_Sewage__c;
                    tempInvoiceItem.AFZA_Sign_Board__c = feeObject[0].AFZA_Sign_Board__c;
                    tempInvoiceItem.AFZA_General_Services__c = feeObject[0].AFZA_General_Services__c;
                    
                    tempInvoiceItem.Advertisement_Fee__c = feeObject[0].Advertisement_Fee__c;
                    tempInvoiceItem.AFZA_Advertisement_Fee__c = feeObject[0].AFZA_Advertisement_Fee__c;
                    tempInvoiceItem.License_Activity_Fee__c = feeObject[0].License_Activity_Fee__c;
                    tempInvoiceItem.AFZA_License_Activity_Fee__c = feeObject[0].AFZA_License_Activity_Fee__c;
                    
                    
                    tempInvoiceItem.Employment_ID_Card__c = feeObject[0].Employment_ID_Card__c;
                    tempInvoiceItem.AFZA_Employment_ID_Card__c = feeObject[0].AFZA_Employment_ID_Card__c;
                        
                    //Now Add the Invoice Item...
                    invoiceLineItemList.add(tempInvoiceItem);
                }
                
                            
            }
             //For Amendment New Key Line Items...
            else if(recordTypeName == 'Location Amendment - New Key')
            {
                List<Service_Price_Book__c> feeObject = new List<Service_Price_Book__c>([Select Advertisement_Fee__c,AFZA_Advertisement_Fee__c,AFZA_Establishment_Card__c,AFZA_Etisalat__c,AFZA_General_Services__c,AFZA_Google__c,AFZA_License_Activity_Fee__c,License_Activity_Fee__c,AFZA_Maintenance__c,AFZA_Sign_Board__c,AFZA_Sewage__c,Employment_ID_Card__c,AFZA_Employment_ID_Card__c,General_Services__c, Establishment_Card__c, Etisalat__c, Google__c, Gov_Establishment_Card__c, Maintenance__c, Sewage__c, Sign_Board__c,AFZA_Charges__c, AFZA_Other_Charges__c,  EMPost_Charges__c, Local_Government_Charges__c,FeeName__c,Price__c, License_Application_Type__c, License_App_Category__c, Id, Fee__c, Fee_Type__c From Service_Price_Book__c where License_Application_Type__c =: 'Amendment' AND License_App_Category__c =: amendmentObject.Account_License__c AND Fee_Type__c =: 'Amendment - New Key' AND isActive__c =: true order by CreatedDate limit 1]);
                    
                if(feeObject.size() > 0)
                {
                    //We only neeed to put the Invoice Line Item...
                    tempInvoiceItem = new Invoice_Line_Item__c();
                    
                    //Mapping the Fields...
                    //tempInvoiceItem.Name = feeObject[0].Name;
                    tempInvoiceItem.Name = feeObject[0].FeeName__c;
                    //tempInvoiceItem.Fee__c = feeObject[0].Id;
                    tempInvoiceItem.Fee__c = feeObject[0].Fee__c;
                    tempInvoiceItem.Service_Price_Book__c = feeObject[0].Id;
                    tempInvoiceItem.Fee_Type__c = feeObject[0].Fee_Type__c;
                    
                    tempInvoiceItem.AFZA_Charges__c = feeObject[0].AFZA_Charges__c;
                    tempInvoiceItem.AFZA_Other_Charges__c = feeObject[0].AFZA_Other_Charges__c;
                    tempInvoiceItem.EMPost_Charges__c = feeObject[0].EMPost_Charges__c;
                    tempInvoiceItem.Local_Government_Charges__c = feeObject[0].Local_Government_Charges__c;
                    
                    tempInvoiceItem.Maintenance__c = feeObject[0].Maintenance__c;
                    tempInvoiceItem.Establishment_Card__c = feeObject[0].Establishment_Card__c;
                    tempInvoiceItem.Etisalat__c = feeObject[0].Etisalat__c;
                    tempInvoiceItem.Google__c = feeObject[0].Google__c;
                    tempInvoiceItem.Gov_Establishment_Card__c = feeObject[0].Gov_Establishment_Card__c;
                    tempInvoiceItem.Sewage__c = feeObject[0].Sewage__c;
                    tempInvoiceItem.Sign_Board__c = feeObject[0].Sign_Board__c;
                    tempInvoiceItem.General_Services__c = feeObject[0].General_Services__c;
                    
                    tempInvoiceItem.AFZA_Maintenance__c = feeObject[0].AFZA_Maintenance__c;
                    tempInvoiceItem.AFZA_Establishment_Card__c = feeObject[0].AFZA_Establishment_Card__c;
                    tempInvoiceItem.AFZA_Etisalat__c = feeObject[0].AFZA_Etisalat__c;
                    tempInvoiceItem.AFZA_Google__c = feeObject[0].AFZA_Google__c;
                    tempInvoiceItem.AFZA_Sewage__c = feeObject[0].AFZA_Sewage__c;
                    tempInvoiceItem.AFZA_Sign_Board__c = feeObject[0].AFZA_Sign_Board__c;
                    tempInvoiceItem.AFZA_General_Services__c = feeObject[0].AFZA_General_Services__c;
                    
                    tempInvoiceItem.Advertisement_Fee__c = feeObject[0].Advertisement_Fee__c;
                    tempInvoiceItem.AFZA_Advertisement_Fee__c = feeObject[0].AFZA_Advertisement_Fee__c;
                    tempInvoiceItem.License_Activity_Fee__c = feeObject[0].License_Activity_Fee__c;
                    tempInvoiceItem.AFZA_License_Activity_Fee__c = feeObject[0].AFZA_License_Activity_Fee__c;
                    
                    
                    tempInvoiceItem.Employment_ID_Card__c = feeObject[0].Employment_ID_Card__c;
                    tempInvoiceItem.AFZA_Employment_ID_Card__c = feeObject[0].AFZA_Employment_ID_Card__c;

                    System.debug('The Number Of Keys => '+ amendmentObject.Keys_Required__c);
                    if(amendmentObject.Keys_Required__c == null || amendmentObject.Keys_Required__c == '')
                        tempInvoiceItem.Amount__c = 0;
                    else
                        tempInvoiceItem.Amount__c = feeObject[0].Price__c * integer.valueof(amendmentObject.Keys_Required__c);
                    
                    //tempInvoiceItem.AFZA_Charges__c = tempInvoiceItem.Amount__c;
                    tempInvoiceItem.Amendment_Charges__c = tempInvoiceItem.Amount__c;
                    tempInvoiceItem.AFZA_Amendment_Charges__c = tempInvoiceItem.Amount__c;
                    //Now Add the Invoice Item...
                    invoiceLineItemList.add(tempInvoiceItem);
                }
                
                
                            
            }
            
            //Now create the Invoice records...
            if(invoiceObjectMap.size() > 0)
            {
                
                //For Guarantee Invoice Remove if its not in placed...
                if(guaranteeInvoiceFlag != true)
                    invoiceObjectMap.remove('Guarantee');
                
                System.debug('The Invoice Object Map Size => '+ invoiceObjectMap.size());
                
                //First Insert the invoice...
                insert invoiceObjectMap.values();
                
                for(Invoice__c tempInvoice : invoiceObjectMap.values())
                {
                    
                    //Putting in the Result List...
                    resultList.add(tempInvoice.Id);
                    
                    //Set the Invoice Id in the Invoice Line Item...
                    if(invoiceLineItemList.size()> 0)
                    {
                        for(Invoice_Line_Item__c invoiceItem : invoiceLineItemList)
                        {
                            //set the Invoice Id...
                            //invoiceItem.Invoice__c = tempInvoice.Id;
                            if(tempInvoice.Invoice_Type__c == 'Guarantee')
                            {
                                //set the Invoice Id...
                                if(invoiceItem.Fee_Type__c == 'Guarantee')
                                    invoiceItem.Invoice__c = tempInvoice.Id;
                            }
                            else if(tempInvoice.Invoice_Type__c == 'Non Refundable')
                            {
                                //set the Invoice Id...
                                if(invoiceItem.Fee_Type__c != 'Guarantee')
                                    invoiceItem.Invoice__c = tempInvoice.Id;
                            }
                            
                            System.debug('The Invoice Item Fee Type => '+ invoiceItem.Fee_Type__c);
                            System.debug('The Invoice Item Invoice Value => '+ invoiceItem.Invoice__c);
                            
                        }   
                        
                    }
                     
                }
                
                //Now Insert the Invoice Line Item...
                if(invoiceLineItemList.size() > 0)
                    insert invoiceLineItemList;
                
                if(isPerformaInvoice == 'False')
                {
                    //For Amendment Object Updation...
                    if(amendmentObject != null)
                    {
                        amendmentObject.Amendment_Application_Status__c = 'Invoice Issued';
                        update amendmentObject;
                        
                    }   
                }
                    
            }
            
        }
        catch(System.Exception exp)
        {
            System.debug('The Error is => '+ exp.getMessage());
            Database.rollback(sp);
            return null;
        
        }
        
        //return the List...
        return resultList;
    } 
    
    
    //For Generating the Invoice Button For Renewal License Methods New Approach (05/March/2012)....Tenancy page Layouts For Renewal....
    //WebService static List<string> generatingInvoiceAndLineItemForRenewalLicenseForTenancyContractMethod(ID accountId,ID tenancyId,string accountExpiryDate,string ActivityType)
    //WebService static List<string> generatingInvoiceAndLineItemForRenewalLicenseForTenancyContractMethod(ID accountId,ID tenancyId,string accountExpiryDate,string ActivityType, List<Payment_Plan__c> paymentPlanList)
    WebService static List<string> generatingInvoiceAndLineItemForRenewalLicenseForTenancyContractMethod(ID accountId,ID tenancyId,string accountExpiryDate,string ActivityType,string isPerformaInvoice, List<Payment_Plan__c> paymentPlanList)
    {
        Savepoint sp = Database.setSavepoint();
        List<Tenancy_Contract__c> locationBookingList = new List<Tenancy_Contract__c>([Select Fine_Per_Day__c,Account_Expiry_Date_Minus_Tenancy_Expiry__c,Account_Expiry_Date_Minus_Today__c,Account_Today_Expiry_Date__c,Account_Expiry_Date__c,Location__r.Rental_Rate__c,Location__r.Fee__r.Name, Location__r.Fee__r.Fee_Type__c, Location__r.Location_type__c,Location__r.Name,Location__r.Id, Location__c, Id, Tenancy_Contract_Expiry_Date__c,  Tenancy_Contract_Start_Date__c, Tenant__c From Tenancy_Contract__c where Tenant__c =: accountId AND (Id =: tenancyId) AND (Tenancy_Contract_Cancellation_Date__c =: null)]);
        List<Fines__c> suspensionInvoiceItemList = new List<Fines__c>([Select Id,Account__c,Status__c, Name,Fines_Included__c, Fine_Total_Amount_Due__c From Fines__c where Fines_Included__c = true and Status__c = 'Approved' AND Account__c =: accountId]); 
        
        List<Service_Price_Book__c> feeObject = new List<Service_Price_Book__c>([Select locationServiceFlag__c,Advertisement_Fee__c,AFZA_Advertisement_Fee__c,AFZA_Establishment_Card__c,AFZA_Etisalat__c,AFZA_General_Services__c,AFZA_Google__c,AFZA_License_Activity_Fee__c,License_Activity_Fee__c,AFZA_Maintenance__c,AFZA_Sign_Board__c,AFZA_Sewage__c,Employment_ID_Card__c,AFZA_Employment_ID_Card__c,General_Services__c, Establishment_Card__c, Etisalat__c, Google__c, Gov_Establishment_Card__c, Maintenance__c, Sewage__c, Sign_Board__c,AFZA_Charges__c, AFZA_Other_Charges__c,  EMPost_Charges__c, Local_Government_Charges__c,FeeName__c,Price__c, License_Application_Type__c, License_App_Category__c, Id, Fee__c, Fee_Type__c From Service_Price_Book__c where License_Application_Type__c =: 'Renewal' AND License_App_Category__c =: ActivityType AND Fee_Type__c =: 'Tenancy Delay Fee For Renewal' AND isActive__c =: true order by CreatedDate limit 1 ]);
        List<Service_Price_Book__c> servicePriceBookLocationList = new List<Service_Price_Book__c>([Select locationServiceFlag__c,Advertisement_Fee__c,AFZA_Advertisement_Fee__c,AFZA_Establishment_Card__c,AFZA_Etisalat__c,AFZA_General_Services__c,AFZA_Google__c,AFZA_License_Activity_Fee__c,License_Activity_Fee__c,AFZA_Maintenance__c,AFZA_Sign_Board__c,AFZA_Sewage__c,Employment_ID_Card__c,AFZA_Employment_ID_Card__c,General_Services__c, Establishment_Card__c, Etisalat__c, Google__c, Gov_Establishment_Card__c, Maintenance__c, Sewage__c, Sign_Board__c,Name, AFZA_Charges__c, AFZA_Other_Charges__c,EMPost_Charges__c, Local_Government_Charges__c,Location_Type__c,FeeName__c,Price__c, License_Application_Type__c,Fee_Type__c,License_App_Category__c, Id, Fee__r.Fee_ID__c, Fee__r.Fee_Type__c, Fee__r.Receipt_Group__c, Fee__r.Price__c, Fee__r.Name, Fee__c, Description__c From Service_Price_Book__c where  License_Application_Type__c =: 'Renewal' AND License_App_Category__c =: 'Location' AND Fee_Type__c != 'Guarantee' AND isActive__c =: true]);
        
        
        Map<string,Invoice__c> invoiceObjectMap = new Map<string,Invoice__c>();
        Invoice_Line_Item__c tempInvoiceItem = null;
        List<Invoice_Line_Item__c> invoiceLineItemList = new List<Invoice_Line_Item__c>();
        list<String> resultList = new list<string>();
        //For Packages...
        Map<string,Payment_Plan__c> paymentPlanMap = new Map<string,Payment_Plan__c>();
        if(paymentPlanList == null)
            paymentPlanList = new List<Payment_Plan__c>();        
        
        try
        {
            //Create a Invoice Object....
            Invoice__c tempInvoiceObject = new Invoice__c();
            tempInvoiceObject.Account__c = accountId;
            tempInvoiceObject.Date__c = System.Today();
            tempInvoiceObject.Invoice_Type__c = 'Non Refundable';
            tempInvoiceObject.Fee_Application_Type__c = 'Tenancy Renewal';
            tempInvoiceObject.Tenancy_Contract__c = tenancyId;
            if(isPerformaInvoice == 'True')
                tempInvoiceObject.isPerformaInvoice__c = true;
            
            //Adding in Map....
            invoiceObjectMap.put('Invoice',tempInvoiceObject);
            System.debug('The Invoice Object => '+ tempInvoiceObject + ' && The Account Expiry Date => '+ accountExpiryDate);
            
            
            //From Suspension Invoice Items....
            if(suspensionInvoiceItemList.size() > 0)
            {
                for(Fines__c suspensionObject : suspensionInvoiceItemList)
                {
                    //We only neeed to put the Invoice Line Item...
                    tempInvoiceItem = new Invoice_Line_Item__c();
                    
                    //Mapping the Fields...
                    tempInvoiceItem.Name = suspensionObject.Name;
                    //tempInvoiceItem.Fee__c = servicePriceBookObject.Fee__r.Id;
                    //tempInvoiceItem.Fee_Type__c = servicePriceBookObject.Fee__r.Fee_Type__c;
                    tempInvoiceItem.Amount__c = suspensionObject.Fine_Total_Amount_Due__c;
                    tempInvoiceItem.Suspension__c = suspensionObject.Id;
                    tempInvoiceItem.Tenancy_Contract__c = tenancyId;
                    //Now Add the Invoice Item...
                    invoiceLineItemList.add(tempInvoiceItem);
                    
                }
            }
            
            //For Location Booking Invoice and Invoice Line Item..
            if(locationBookingList.size() > 0)
            {
                //For Package Selection Adding in the Map...
                if(paymentPlanList.size() > 0)
                {
                    for(Payment_Plan__c paymentPlanObject : paymentPlanList)
                    {
                        //Adding in the Map...
                        paymentPlanMap.put(paymentPlanObject.Location_Type__c,paymentPlanObject);
                    }
                }
                
                
                for(Tenancy_Contract__c bookingObject : locationBookingList)
                {
                    System.debug('The Start Date => '+bookingObject.Tenancy_Contract_Expiry_Date__c );
                    //Now Create a Invoice Line Item for this...
                    Invoice_Line_Item__c tempInvoiceItem1 = new Invoice_Line_Item__c();
                    //Mapping the Fields....
                    //tempInvoiceItem1.Name = bookingObject.Location__r.Fee__r.Name;
                    //tempInvoiceItem1.Name = bookingObject.Location__r.Name + ' Rental Rate';
                    tempInvoiceItem1.Name = 'Location Rental Rate';
                    //tempInvoiceItem1.Fee__c = bookingObject.Location__r.Fee__r.Id;
                    //tempInvoiceItem1.Fee_Type__c = bookingObject.Location__r.Fee__r.Fee_Type__c;
                    tempInvoiceItem1.Fee_Type__c = 'Non Refundable';
                    //tempInvoiceItem1.Start_Date__c = bookingObject.Account_Expiry_Date__c;
                    
                    
                    //tempInvoiceItem1.Start_Date__c = bookingObject.Tenancy_Contract_Expiry_Date__c.adddays(1);
                    
                    tempInvoiceItem1.Start_Date__c = bookingObject.Tenancy_Contract_Expiry_Date__c.adddays(1);
                    //tempInvoiceItem1.End_Date__c = bookingObject.Tenancy_Contract_Expiry_Date__c;
                    tempInvoiceItem1.End_Date__c = bookingObject.Account_Expiry_Date__c; 
                    //tempInvoiceItem1.Amount__c = ((bookingObject.Location__r.Rental_Rate__c / 360) * (bookingObject.Account_Expiry_Date__c).addyears(1).daysbetween(bookingObject.Tenancy_Contract_Expiry_Date__c) );
                    tempInvoiceItem1.Tenancy_Contract__c = bookingObject.Id;
                    tempInvoiceItem1.Location__c = bookingObject.Location__c;
                    //Decimal daybetweenValue = (Decimal.valueof(bookingObject.Account_Expiry_Date__c.addyears(1).daysbetween(bookingObject.Tenancy_Contract_Expiry_Date__c))).setScale(2);   
                    //Decimal daybetweenValue = (Decimal.valueof(bookingObject.Tenancy_Contract_Expiry_Date__c.daysbetween(bookingObject.Account_Expiry_Date__c.addyears(1)))).setScale(2);
                    Decimal daybetweenValue = (Decimal.valueof(bookingObject.Tenancy_Contract_Expiry_Date__c.daysbetween(bookingObject.Account_Expiry_Date__c))).setScale(2);
                    tempInvoiceItem1.Amount__c = ((bookingObject.Location__r.Rental_Rate__c / 360) * (daybetweenValue)).setScale(2);
                    
                    //Previous Approach 4/June/2012...
                    /*tempInvoiceItem1.Start_Date__c = System.today().adddays(1);
                    //tempInvoiceItem1.End_Date__c = bookingObject.Tenancy_Contract_Expiry_Date__c;
                    tempInvoiceItem1.End_Date__c = bookingObject.Account_Expiry_Date__c; 
                    //tempInvoiceItem1.Amount__c = ((bookingObject.Location__r.Rental_Rate__c / 360) * (bookingObject.Account_Expiry_Date__c).addyears(1).daysbetween(bookingObject.Tenancy_Contract_Expiry_Date__c) );
                    tempInvoiceItem1.Tenancy_Contract__c = bookingObject.Id;
                    tempInvoiceItem1.Location__c = bookingObject.Location__c;
                    //Decimal daybetweenValue = (Decimal.valueof(bookingObject.Account_Expiry_Date__c.addyears(1).daysbetween(bookingObject.Tenancy_Contract_Expiry_Date__c))).setScale(2);   
                    //Decimal daybetweenValue = (Decimal.valueof(bookingObject.Tenancy_Contract_Expiry_Date__c.daysbetween(bookingObject.Account_Expiry_Date__c.addyears(1)))).setScale(2);
                    Decimal daybetweenValue = (Decimal.valueof(System.today().daysbetween(bookingObject.Account_Expiry_Date__c))).setScale(2);
                    tempInvoiceItem1.Amount__c = ((bookingObject.Location__r.Rental_Rate__c / 360) * (daybetweenValue)).setScale(2);*/
                    
                    //Previous Approach before 31/May/2012...           
                    /*tempInvoiceItem1.Start_Date__c = System.today().adddays(1);
                    //tempInvoiceItem1.End_Date__c = bookingObject.Tenancy_Contract_Expiry_Date__c;
                    tempInvoiceItem1.End_Date__c = bookingObject.Account_Expiry_Date__c; 
                    //tempInvoiceItem1.Amount__c = ((bookingObject.Location__r.Rental_Rate__c / 360) * (bookingObject.Account_Expiry_Date__c).addyears(1).daysbetween(bookingObject.Tenancy_Contract_Expiry_Date__c) );
                    tempInvoiceItem1.Tenancy_Contract__c = bookingObject.Id;
                    tempInvoiceItem1.Location__c = bookingObject.Location__c;
                    //Decimal daybetweenValue = (Decimal.valueof(bookingObject.Account_Expiry_Date__c.addyears(1).daysbetween(bookingObject.Tenancy_Contract_Expiry_Date__c))).setScale(2);   
                    //Decimal daybetweenValue = (Decimal.valueof(bookingObject.Tenancy_Contract_Expiry_Date__c.daysbetween(bookingObject.Account_Expiry_Date__c.addyears(1)))).setScale(2);
                    Decimal daybetweenValue = (Decimal.valueof(System.today().adddays(1).daysbetween(bookingObject.Account_Expiry_Date__c))).setScale(2);
                    tempInvoiceItem1.Amount__c = ((bookingObject.Location__r.Rental_Rate__c / 360) * (daybetweenValue)).setScale(2);*/
                    
                    
                    //Previous Approach before 22nd/May/2012...
                    /*tempInvoiceItem1.Start_Date__c = bookingObject.Tenancy_Contract_Expiry_Date__c;
                    //tempInvoiceItem1.End_Date__c = bookingObject.Tenancy_Contract_Expiry_Date__c;
                    tempInvoiceItem1.End_Date__c = bookingObject.Account_Expiry_Date__c; 
                    //tempInvoiceItem1.Amount__c = ((bookingObject.Location__r.Rental_Rate__c / 360) * (bookingObject.Account_Expiry_Date__c).addyears(1).daysbetween(bookingObject.Tenancy_Contract_Expiry_Date__c) );
                    tempInvoiceItem1.Tenancy_Contract__c = bookingObject.Id;
                    tempInvoiceItem1.Location__c = bookingObject.Location__c;
                    //Decimal daybetweenValue = (Decimal.valueof(bookingObject.Account_Expiry_Date__c.addyears(1).daysbetween(bookingObject.Tenancy_Contract_Expiry_Date__c))).setScale(2);   
                    //Decimal daybetweenValue = (Decimal.valueof(bookingObject.Tenancy_Contract_Expiry_Date__c.daysbetween(bookingObject.Account_Expiry_Date__c.addyears(1)))).setScale(2);
                    Decimal daybetweenValue = (Decimal.valueof(bookingObject.Tenancy_Contract_Expiry_Date__c.daysbetween(bookingObject.Account_Expiry_Date__c))).setScale(2);
                    tempInvoiceItem1.Amount__c = ((bookingObject.Location__r.Rental_Rate__c / 360) * (daybetweenValue)).setScale(2);*/
                    
                    //Package Selection...
                    if(paymentPlanMap.get(bookingObject.Location__r.Location_Type__c) != null)
                    {
                        tempInvoiceItem1.Name = 'Location Rental Rate ' + paymentPlanMap.get(bookingObject.Location__r.Location_Type__c).Installment__c + ' Installments' ;
                        tempInvoiceItem1.Location_Rental_Rate__c = tempInvoiceItem1.Amount__c + (tempInvoiceItem1.Amount__c * paymentPlanMap.get(bookingObject.Location__r.Location_Type__c).Sign_Multiplier__c * (paymentPlanMap.get(bookingObject.Location__r.Location_Type__c).Percent__c / 100)    );
                        tempInvoiceItem1.Payment_Plan__c = paymentPlanMap.get(bookingObject.Location__r.Location_Type__c).Id;
                        tempInvoiceItem1.Package_Value__c = tempInvoiceItem1.Amount__c * paymentPlanMap.get(bookingObject.Location__r.Location_Type__c).Sign_Multiplier__c * (paymentPlanMap.get(bookingObject.Location__r.Location_Type__c).Percent__c / 100);
                        tempInvoiceItem1.Installment__c = paymentPlanMap.get(bookingObject.Location__r.Location_Type__c).Installment__c;
                        tempInvoiceItem1.Action__c = paymentPlanMap.get(bookingObject.Location__r.Location_Type__c).Action__c;
                        
                        //The Amouunt
                        tempInvoiceItem1.Amount__c = tempInvoiceItem1.Amount__c + (tempInvoiceItem1.Amount__c * paymentPlanMap.get(bookingObject.Location__r.Location_Type__c).Sign_Multiplier__c * (paymentPlanMap.get(bookingObject.Location__r.Location_Type__c).Percent__c / 100)    );
                    }
                    
                    
                    //Check if Amount is Negative value or not...
                    if(tempInvoiceItem1.Amount__c < 0)
                        tempInvoiceItem1.Amount__c = 0;
                    
                    
                    tempInvoiceItem1.Location_Rental_Rate__c =  tempInvoiceItem1.Amount__c;
                    tempInvoiceItem1.AFZA_Location_Rental_Rate__c =  tempInvoiceItem1.Amount__c;
                    
                    //Now Add the Invoice Item in List...
                    invoiceLineItemList.add(tempInvoiceItem1);
                    
                    //Updating the Tenancy contract Renewal Step...
                    bookingObject.Tenancy_Contract_Renewal_Steps__c = 'Invoice Issued';
                    
                    //For Fine Calculation...
                    if(bookingObject.Tenancy_Contract_Expiry_Date__c < System.today())
                    {
                        if(feeObject.size() > 0)
                        {
                            Invoice_Line_Item__c tempInvoiceItem2 = new Invoice_Line_Item__c();
                            //Mapping the Fields...
                            //tempInvoiceItem2.Name = bookingObject.Location__r.Name + ' Tenancy Delay Fee';
                            tempInvoiceItem2.Name = feeObject[0].FeeName__c;
                            //tempInvoiceItem.Fee__c = feeObject[0].Id;
                            tempInvoiceItem2.Fee__c = feeObject[0].Fee__c;
                            tempInvoiceItem2.Service_Price_Book__c = feeObject[0].Id;
                            tempInvoiceItem2.Location__c = bookingObject.Location__c;
                            tempInvoiceItem2.Tenancy_Contract__c = bookingObject.Id;
                            tempInvoiceItem2.Fee_Type__c = feeObject[0].Fee_Type__c;
                            
                            tempInvoiceItem2.AFZA_Charges__c = feeObject[0].AFZA_Charges__c;
                            tempInvoiceItem2.AFZA_Other_Charges__c = feeObject[0].AFZA_Other_Charges__c;
                            tempInvoiceItem2.EMPost_Charges__c = feeObject[0].EMPost_Charges__c;
                            tempInvoiceItem2.Local_Government_Charges__c = feeObject[0].Local_Government_Charges__c;
                            
                            tempInvoiceItem2.Maintenance__c = feeObject[0].Maintenance__c;
                            tempInvoiceItem2.Establishment_Card__c = feeObject[0].Establishment_Card__c;
                            tempInvoiceItem2.Etisalat__c = feeObject[0].Etisalat__c;
                            tempInvoiceItem2.Google__c = feeObject[0].Google__c;
                            tempInvoiceItem2.Gov_Establishment_Card__c = feeObject[0].Gov_Establishment_Card__c;
                            tempInvoiceItem2.Sewage__c = feeObject[0].Sewage__c;
                            tempInvoiceItem2.Sign_Board__c = feeObject[0].Sign_Board__c;
                            tempInvoiceItem2.General_Services__c = feeObject[0].General_Services__c;
                            
                            tempInvoiceItem2.AFZA_Maintenance__c = feeObject[0].AFZA_Maintenance__c;
                            tempInvoiceItem2.AFZA_Establishment_Card__c = feeObject[0].AFZA_Establishment_Card__c;
                            tempInvoiceItem2.AFZA_Etisalat__c = feeObject[0].AFZA_Etisalat__c;
                            tempInvoiceItem2.AFZA_Google__c = feeObject[0].AFZA_Google__c;
                            tempInvoiceItem2.AFZA_Sewage__c = feeObject[0].AFZA_Sewage__c;
                            tempInvoiceItem2.AFZA_Sign_Board__c = feeObject[0].AFZA_Sign_Board__c;
                            tempInvoiceItem2.AFZA_General_Services__c = feeObject[0].AFZA_General_Services__c;
                            
                            tempInvoiceItem2.Advertisement_Fee__c = feeObject[0].Advertisement_Fee__c;
                            tempInvoiceItem2.AFZA_Advertisement_Fee__c = feeObject[0].AFZA_Advertisement_Fee__c;
                            tempInvoiceItem2.License_Activity_Fee__c = feeObject[0].License_Activity_Fee__c;
                            tempInvoiceItem2.AFZA_License_Activity_Fee__c = feeObject[0].AFZA_License_Activity_Fee__c;
                            
                            
                            tempInvoiceItem2.Employment_ID_Card__c = feeObject[0].Employment_ID_Card__c;
                            tempInvoiceItem2.AFZA_Employment_ID_Card__c = feeObject[0].AFZA_Employment_ID_Card__c;
                            
                            tempInvoiceItem2.Start_Date__c = bookingObject.Tenancy_Contract_Expiry_Date__c;
                            tempInvoiceItem2.End_Date__c =  System.today();
                            //tempInvoiceItem.Amount__c = feeObject.Price__c * (System.today().daysbetween(bookingObject.Tenancy_Contract_Expiry_Date__c)/30);
                            //tempInvoiceItem.Amount__c = (((bookingObject.Location__r.Rental_Rate__c / 360)* 0.2) * ((bookingObject.Tenancy_Contract_Expiry_Date__c.daysbetween(System.Today()))/30));
                            //Fine_Per_Day__c
                            Decimal daybetweenValue1 = (Decimal.valueof(bookingObject.Tenancy_Contract_Expiry_Date__c.daysbetween(System.today().adddays(1)))).round(system.roundingMode.CEILING);
                            //tempInvoiceItem.Amount__c = feeObject[0].Price__c * (daybetweenValue);
                            System.debug('Fine Per Day For Tenancy ');
                            //Decimal finalValue = (bookingObject.Fine_Per_Day__c * (daybetweenValue1)).round(system.roundingMode.CEILING);
                            tempInvoiceItem2.Amount__c = (bookingObject.Fine_Per_Day__c * (daybetweenValue1)).round(system.roundingMode.CEILING);
                            
                            //Previous Approach 22nd May 2012...
                            /*//tempInvoiceItem.Amount__c = feeObject[0].Price__c * (System.today().daysbetween(bookingObject.Tenancy_Contract_Expiry_Date__c)/30);
                            //tempInvoiceItem2.Amount__c = (((bookingObject.Location__r.Rental_Rate__c / 360)* 0.2) * ((System.today().daysbetween(bookingObject.Tenancy_Contract_Expiry_Date__c))+ 1));
                            
                            Decimal daybetweenValue1 = (Decimal.valueof(bookingObject.Tenancy_Contract_Expiry_Date__c.daysbetween(System.today()))/30).round(system.roundingMode.CEILING);
                            tempInvoiceItem2.Amount__c = feeObject[0].Price__c * (daybetweenValue1);*/
                            
                            
                            //Check if Amount is Negative value or not...
                            if(tempInvoiceItem2.Amount__c < 0)
                                tempInvoiceItem2.Amount__c = 0;
                                
                            //tempInvoiceItem2.AFZA_Charges__c = tempInvoiceItem2.Amount__c;
                            tempInvoiceItem2.Tenancy_Contract_Delay_Fee__c = tempInvoiceItem2.Amount__c;
                            tempInvoiceItem2.AFZA_Tenancy_Contract_Delay_Fee__c = tempInvoiceItem2.Amount__c;
                            
                            //Adding in the Location...
                            invoiceLineItemList.add(tempInvoiceItem2);  
                            
                        }
                        
                    }
                    
                    System.debug('The Booking Object Id => '+ bookingObject.Id);
                    
                    //Also doing for the location... 
                    if(servicePriceBookLocationList.size() > 0)
                    {
                        //For the Service Price Book....
                        for(Service_Price_Book__c tempServiceObject : servicePriceBookLocationList)
                        {
                            //Office, Warehouse, Accomodation,Land, E.O.C, Shop...
                            if(tempServiceObject.Location_Type__c == bookingObject.Location__r.Location_Type__c)        
                            {
                                //Now Create a Invoice Line Item for this...
                                tempInvoiceItem = new Invoice_Line_Item__c();
                                //Mapping the Fields....
                                //tempInvoiceItem.Name = bookingObject.Location__r.Name + ' - '+ tempServiceObject.Location_Type__c +' Rental Rate';
                                //tempInvoiceItem.Name = bookingObject.Location__r.Name + ' - '+ tempServiceObject.FeeName__c +' Rental Rate';
                                //tempInvoiceItem.Name = tempServiceObject.FeeName__c +' Rental Rate';
                                tempInvoiceItem.Name = tempServiceObject.FeeName__c;
                                tempInvoiceItem.Fee__c = tempServiceObject.Fee__c;
                                tempInvoiceItem.Location__c = bookingObject.Location__c;
                                tempInvoiceItem.Service_Price_Book__c = tempServiceObject.Id;
                                tempInvoiceItem.Tenancy_Contract__c = bookingObject.id;
                                tempInvoiceItem.Fee_Type__c = tempServiceObject.Fee_Type__c;
                                //For Checking the Location Service Calculations...
                                if(tempServiceObject.locationServiceFlag__c == 'False')
                                    tempInvoiceItem.Amount__c = tempServiceObject.Price__c;
                                else
                                {
                                    Decimal daybetweenValue2 = (Decimal.valueof(bookingObject.Tenancy_Contract_Expiry_Date__c.daysbetween(bookingObject.Account_Expiry_Date__c))).setScale(2);
                                    tempInvoiceItem.Amount__c = ((tempServiceObject.Price__c / 360) * (daybetweenValue2)).round(system.roundingMode.CEILING);
                                    
                                    tempInvoiceItem.Start_Date__c = bookingObject.Tenancy_Contract_Expiry_Date__c;
                                    tempInvoiceItem.End_Date__c = System.Today();               
                                }   
                                
                                //tempInvoiceItem.Start_Date__c = bookingObject.Account_Expiry_Date__c;
                                //tempInvoiceItem.End_Date__c = bookingObject.Tenancy_Contract_Expiry_Date__c;
                                
                                tempInvoiceItem.AFZA_Charges__c = tempServiceObject.AFZA_Charges__c;
                                tempInvoiceItem.AFZA_Other_Charges__c = tempServiceObject.AFZA_Other_Charges__c;
                                tempInvoiceItem.EMPost_Charges__c = tempServiceObject.EMPost_Charges__c;
                                tempInvoiceItem.Local_Government_Charges__c = tempServiceObject.Local_Government_Charges__c;
                                
                                tempInvoiceItem.Maintenance__c = tempServiceObject.Maintenance__c;
                                tempInvoiceItem.Establishment_Card__c = tempServiceObject.Establishment_Card__c;
                                tempInvoiceItem.Etisalat__c = tempServiceObject.Etisalat__c;
                                tempInvoiceItem.Google__c = tempServiceObject.Google__c;
                                tempInvoiceItem.Gov_Establishment_Card__c = tempServiceObject.Gov_Establishment_Card__c;
                                tempInvoiceItem.Sewage__c = tempServiceObject.Sewage__c;
                                tempInvoiceItem.Sign_Board__c = tempServiceObject.Sign_Board__c;
                                tempInvoiceItem.General_Services__c = tempServiceObject.General_Services__c;
                                
                                
                                tempInvoiceItem.AFZA_Maintenance__c = tempServiceObject.AFZA_Maintenance__c;
                                tempInvoiceItem.AFZA_Establishment_Card__c = tempServiceObject.AFZA_Establishment_Card__c;
                                tempInvoiceItem.AFZA_Etisalat__c = tempServiceObject.AFZA_Etisalat__c;
                                tempInvoiceItem.AFZA_Google__c = tempServiceObject.AFZA_Google__c;
                                tempInvoiceItem.AFZA_Sewage__c = tempServiceObject.AFZA_Sewage__c;
                                tempInvoiceItem.AFZA_Sign_Board__c = tempServiceObject.AFZA_Sign_Board__c;
                                tempInvoiceItem.AFZA_General_Services__c = tempServiceObject.AFZA_General_Services__c;
                                
                                tempInvoiceItem.Advertisement_Fee__c = tempServiceObject.Advertisement_Fee__c;
                                tempInvoiceItem.AFZA_Advertisement_Fee__c = tempServiceObject.AFZA_Advertisement_Fee__c;
                                tempInvoiceItem.License_Activity_Fee__c = tempServiceObject.License_Activity_Fee__c;
                                tempInvoiceItem.AFZA_License_Activity_Fee__c = tempServiceObject.AFZA_License_Activity_Fee__c;
                                
                                tempInvoiceItem.Employment_ID_Card__c = tempServiceObject.Employment_ID_Card__c;
                                tempInvoiceItem.AFZA_Employment_ID_Card__c = tempServiceObject.AFZA_Employment_ID_Card__c;
                                
                                //Now Add the Invoice Item in List...
                                invoiceLineItemList.add(tempInvoiceItem);
                                
                            }
                        }
                    }
                    
                }
                
            }
            
            //Now create the Invoice records...
            if(invoiceObjectMap.size() > 0)
            {
                System.debug('The Invoice Size => '+ invoiceObjectMap.size());
                //First Insert the invoice...
                insert invoiceObjectMap.values();
                
                for(Invoice__c tempInvoice : invoiceObjectMap.values())
                {
                    //Putting in the Result List...
                    resultList.add(tempInvoice.Id);
                    
                    //Set the Invoice Id in the Invoice Line Item...
                    if(invoiceLineItemList.size()> 0)
                    {
                        for(Invoice_Line_Item__c invoiceItem : invoiceLineItemList)
                        {
                            //set the Invoice Id...
                            invoiceItem.Invoice__c = tempInvoice.Id;
                        }   
                        
                    }
                     
                }
                //Now Insert the Invoice Line Item...
                if(invoiceLineItemList.size() > 0)
                    insert invoiceLineItemList;
                
                if(isPerformaInvoice == 'False')
                {
                    //now Update the Renewal Step for the Tenancy Contract...    
                    if(locationBookingList.size() > 0)    
                        update locationBookingList; 
                }
                
                    
            }
            
            //Returning the Value...
            return resultList;    
        }
        catch(System.Exception exp)
        {
            System.debug('The Exception Line Number => ' + exp.getLineNumber()+'  The Exception is => '+ exp.getMessage());
            Database.rollback(sp);
            return null;
        
        }
        
    }
    
    //For Generating the Invoice Button For Cancellation License Methods ....
    //WebService static List<string> generatingInvoiceAndLineItemForCancellationLicenseMethod(ID accountId, string licenseApplicationType, string ActivityType,string accountExpiryDate)
    WebService static List<string> generatingInvoiceAndLineItemForCancellationLicenseMethod(ID accountId, string licenseApplicationType, string ActivityType,string accountExpiryDate,string isPerformaInvoice)
    {
        Savepoint sp = Database.setSavepoint();
        //Query to the Service Price Book For Invoice and Invoice Line Item...
        List<Service_Price_Book__c> servicePriceBookList = new List<Service_Price_Book__c>([Select Advertisement_Fee__c,AFZA_Advertisement_Fee__c,AFZA_Establishment_Card__c,AFZA_Etisalat__c,AFZA_General_Services__c,AFZA_Google__c,AFZA_License_Activity_Fee__c,License_Activity_Fee__c,AFZA_Maintenance__c,AFZA_Sign_Board__c,AFZA_Sewage__c,Employment_ID_Card__c,AFZA_Employment_ID_Card__c,General_Services__c, Establishment_Card__c, Etisalat__c, Google__c, Gov_Establishment_Card__c, Maintenance__c, Sewage__c, Sign_Board__c,AFZA_Charges__c, AFZA_Other_Charges__c,   EMPost_Charges__c, Local_Government_Charges__c,Name,FeeName__c, License_Application_Type__c,License_App_Category__c, Id, Fee__r.Fee_ID__c, Fee__r.Fee_Type__c, Fee__r.Receipt_Group__c, Fee__r.Price__c, Fee__r.Name, Fee__c, Description__c From Service_Price_Book__c where  License_Application_Type__c =: licenseApplicationType AND License_App_Category__c =: ActivityType AND Fee_Type__c =: 'Non Refundable' AND isActive__c =: true]);
        List<Tenancy_Contract__c> locationBookingList = new List<Tenancy_Contract__c>([Select Fine_Per_Day__c,Rental_Rate__c,Account_Expiry_Date_Minus_Tenancy_Expiry__c,Account_Expiry_Date_Minus_Today__c,Account_Today_Expiry_Date__c,Account_Expiry_Date__c,Location__r.Rental_Rate__c,Location__r.Fee__r.Name, Location__r.Fee__r.Fee_Type__c, Location__r.Name,Location__r.Id, Location__c, Id, Tenancy_Contract_Expiry_Date__c,  Tenancy_Contract_Start_Date__c, Tenant__c From Tenancy_Contract__c where Tenant__c =: accountId AND (Tenancy_Contract_Cancellation_Date__c =: null)]);
        List<Fines__c> suspensionInvoiceItemList = new List<Fines__c>([Select Id,Account__c,Status__c, Name,Fines_Included__c, Fine_Total_Amount_Due__c From Fines__c where Fines_Included__c = true and Status__c = 'Approved' AND Account__c =: accountId]); 
        List<Service_Price_Book__c> feeObject = new List<Service_Price_Book__c>([Select Advertisement_Fee__c,AFZA_Advertisement_Fee__c,AFZA_Establishment_Card__c,AFZA_Etisalat__c,AFZA_General_Services__c,AFZA_Google__c,AFZA_License_Activity_Fee__c,License_Activity_Fee__c,AFZA_Maintenance__c,AFZA_Sign_Board__c,AFZA_Sewage__c,Employment_ID_Card__c,AFZA_Employment_ID_Card__c,General_Services__c, Establishment_Card__c, Etisalat__c, Google__c, Gov_Establishment_Card__c, Maintenance__c, Sewage__c, Sign_Board__c,AFZA_Charges__c, AFZA_Other_Charges__c,  EMPost_Charges__c, Local_Government_Charges__c,FeeName__c,Price__c, License_Application_Type__c, License_App_Category__c, Id, Fee__c, Fee_Type__c From Service_Price_Book__c where License_Application_Type__c =: licenseApplicationType AND License_App_Category__c =: ActivityType AND Fee_Type__c =: 'Tenancy Delay Fee For Cancellation' AND isActive__c =: true order by CreatedDate limit 1 ]);
        //feeObject = [Select FeeName__c,Price__c, License_Application_Type__c, License_App_Category__c, Id, Fee__c, Fee_Type__c From Service_Price_Book__c where License_Application_Type__c =: licenseApplicationType AND License_App_Category__c =: ActivityType AND Fee_Type__c =: 'Tenancy Delay Fee For Cancellation' order by CreatedDate limit 1 ];
        
        Map<string,Invoice__c> invoiceObjectMap = new Map<string,Invoice__c>();
        Invoice_Line_Item__c tempInvoiceItem = null;
        List<Invoice_Line_Item__c> invoiceLineItemList = new List<Invoice_Line_Item__c>();
        list<String> resultList = new list<string>(); 
        
        try
        {
            //Create a Invoice Object....
            Invoice__c tempInvoiceObject = new Invoice__c();
            tempInvoiceObject.Account__c = accountId;
            tempInvoiceObject.Date__c = System.Today();
            tempInvoiceObject.Invoice_Type__c = 'Non Refundable';
            tempInvoiceObject.Fee_Application_Type__c = 'Cancellation';
            
            if(isPerformaInvoice == 'True')
                tempInvoiceObject.isPerformaInvoice__c = true;
            
            //Adding in Map....
            invoiceObjectMap.put('Invoice',tempInvoiceObject);
            System.debug('The Invoice Object => '+ tempInvoiceObject + ' && The Account Expiry Date => '+ accountExpiryDate);
            
            System.debug('The Date Account Expiry Date => '+ Date.parse(accountExpiryDate));
            //For License Fine...
            if(Date.parse(accountExpiryDate) < System.Today())
            {
                //We only neeed to put the Invoice Line Item...
               
                List<Service_Price_Book__c> feeObject1 = new List<Service_Price_Book__c>([Select Advertisement_Fee__c,AFZA_Advertisement_Fee__c,AFZA_Establishment_Card__c,AFZA_Etisalat__c,AFZA_General_Services__c,AFZA_Google__c,AFZA_License_Activity_Fee__c,License_Activity_Fee__c,AFZA_Maintenance__c,AFZA_Sign_Board__c,AFZA_Sewage__c,Employment_ID_Card__c,AFZA_Employment_ID_Card__c,General_Services__c, Establishment_Card__c, Etisalat__c, Google__c, Gov_Establishment_Card__c, Maintenance__c, Sewage__c, Sign_Board__c,AFZA_Charges__c, AFZA_Other_Charges__c, EMPost_Charges__c, Local_Government_Charges__c,FeeName__c,Price__c, License_Application_Type__c, License_App_Category__c, Id, Fee__c, Fee_Type__c From Service_Price_Book__c where License_Application_Type__c =: licenseApplicationType AND License_App_Category__c =: ActivityType AND Fee_Type__c =: 'License Delay Fee For Cancellation' order by CreatedDate LIMIT 1]);
                //feeObject1 = [Select FeeName__c,Price__c, License_Application_Type__c, License_App_Category__c, Id, Fee__c, Fee_Type__c From Service_Price_Book__c where License_Application_Type__c =: licenseApplicationType AND License_App_Category__c =: ActivityType AND Fee_Type__c =: 'License Delay Fee For Cancellation' order by CreatedDate limit 1 ];
                
                if(feeObject1.size() > 0)
                {
                    
                    tempInvoiceItem = new Invoice_Line_Item__c();
                
                    //Mapping the Fields...
                    tempInvoiceItem.Name = feeObject1[0].FeeName__c;
                    //tempInvoiceItem.Fee__c = feeObject1.Id;
                    tempInvoiceItem.Fee__c = feeObject1[0].Fee__c;
                    tempInvoiceItem.Service_Price_Book__c = feeObject1[0].Id;
                    tempInvoiceItem.Fee_Type__c = feeObject1[0].Fee_Type__c;
                    //tempInvoiceItem.Amount__c = feeObject1.Price__c * (System.today().daysbetween(Date.parse(accountExpiryDate))/30);
                    
                    tempInvoiceItem.AFZA_Charges__c = feeObject1[0].AFZA_Charges__c;
                    tempInvoiceItem.AFZA_Other_Charges__c = feeObject1[0].AFZA_Other_Charges__c;
                    tempInvoiceItem.EMPost_Charges__c = feeObject1[0].EMPost_Charges__c;
                    tempInvoiceItem.Local_Government_Charges__c = feeObject1[0].Local_Government_Charges__c;
                    
                    tempInvoiceItem.Maintenance__c = feeObject1[0].Maintenance__c;
                    tempInvoiceItem.Establishment_Card__c = feeObject1[0].Establishment_Card__c;
                    tempInvoiceItem.Etisalat__c = feeObject1[0].Etisalat__c;
                    tempInvoiceItem.Google__c = feeObject1[0].Google__c;
                    tempInvoiceItem.Gov_Establishment_Card__c = feeObject1[0].Gov_Establishment_Card__c;
                    tempInvoiceItem.Sewage__c = feeObject1[0].Sewage__c;
                    tempInvoiceItem.Sign_Board__c = feeObject1[0].Sign_Board__c;
                    tempInvoiceItem.General_Services__c = feeObject1[0].General_Services__c;
                    
                    tempInvoiceItem.AFZA_Maintenance__c = feeObject1[0].AFZA_Maintenance__c;
                    tempInvoiceItem.AFZA_Establishment_Card__c = feeObject1[0].AFZA_Establishment_Card__c;
                    tempInvoiceItem.AFZA_Etisalat__c = feeObject1[0].AFZA_Etisalat__c;
                    tempInvoiceItem.AFZA_Google__c = feeObject1[0].AFZA_Google__c;
                    tempInvoiceItem.AFZA_Sewage__c = feeObject1[0].AFZA_Sewage__c;
                    tempInvoiceItem.AFZA_Sign_Board__c = feeObject1[0].AFZA_Sign_Board__c;
                    tempInvoiceItem.AFZA_General_Services__c = feeObject1[0].AFZA_General_Services__c;
                    
                    tempInvoiceItem.Advertisement_Fee__c = feeObject1[0].Advertisement_Fee__c;
                    tempInvoiceItem.AFZA_Advertisement_Fee__c = feeObject1[0].AFZA_Advertisement_Fee__c;
                    tempInvoiceItem.License_Activity_Fee__c = feeObject1[0].License_Activity_Fee__c;
                    tempInvoiceItem.AFZA_License_Activity_Fee__c = feeObject1[0].AFZA_License_Activity_Fee__c;
                    
                    
                    tempInvoiceItem.Employment_ID_Card__c = feeObject1[0].Employment_ID_Card__c;
                    tempInvoiceItem.AFZA_Employment_ID_Card__c = feeObject1[0].AFZA_Employment_ID_Card__c;
                    
                    tempInvoiceItem.Start_Date__c = Date.parse(accountExpiryDate);
                    tempInvoiceItem.End_Date__c = System.today();
                    
                    
                    Decimal daybetweenValue = (Decimal.valueof(Date.parse(accountExpiryDate).daysbetween(System.today()))/30).round(system.roundingMode.CEILING);
                    //tempInvoiceItem.Amount__c = feeObject1.Price__c * (Decimal.valueof(Date.parse(accountExpiryDate).daysbetween(System.today()))/30);
                    tempInvoiceItem.Amount__c = feeObject1[0].Price__c * (daybetweenValue);
                    
                    //Check if Amount is Negative value or not...
                    if(tempInvoiceItem.Amount__c < 0)
                        tempInvoiceItem.Amount__c = 0;
                        
                    //tempInvoiceItem.AFZA_Charges__c = tempInvoiceItem.Amount__c;   
                    tempInvoiceItem.License_Delay_Fee__c = tempInvoiceItem.Amount__c;
                    tempInvoiceItem.AFZA_License_Delay_Fee__c = tempInvoiceItem.Amount__c;
                    
                     
                    //Adding in the Location...
                    invoiceLineItemList.add(tempInvoiceItem);
                }
                
                List<Service_Price_Book__c> feeObject2 = new List<Service_Price_Book__c>([Select Advertisement_Fee__c,AFZA_Advertisement_Fee__c,AFZA_Establishment_Card__c,AFZA_Etisalat__c,AFZA_General_Services__c,AFZA_Google__c,AFZA_License_Activity_Fee__c,License_Activity_Fee__c,AFZA_Maintenance__c,AFZA_Sign_Board__c,AFZA_Sewage__c,Employment_ID_Card__c,AFZA_Employment_ID_Card__c,General_Services__c, Establishment_Card__c, Etisalat__c, Google__c, Gov_Establishment_Card__c, Maintenance__c, Sewage__c, Sign_Board__c,AFZA_Charges__c, AFZA_Other_Charges__c, EMPost_Charges__c, Local_Government_Charges__c,FeeName__c,Price__c, License_Application_Type__c, License_App_Category__c, Id, Fee__c, Fee_Type__c From Service_Price_Book__c where License_Application_Type__c =: licenseApplicationType AND License_App_Category__c =: ActivityType AND Fee_Type__c =: 'Late Cancellation Fee' order by CreatedDate LIMIT 1]);
                
                if(feeObject2.size() > 0)
                {
                    
                    tempInvoiceItem = new Invoice_Line_Item__c();
                
                    //Mapping the Fields...
                    tempInvoiceItem.Name = feeObject2[0].FeeName__c;
                    tempInvoiceItem.Fee__c = feeObject2[0].Fee__c;
                    tempInvoiceItem.Service_Price_Book__c = feeObject2[0].Id;
                    tempInvoiceItem.Fee_Type__c = feeObject2[0].Fee_Type__c;
                    tempInvoiceItem.Amount__c = feeObject2[0].Price__c;
                    
                    tempInvoiceItem.AFZA_Charges__c = feeObject2[0].AFZA_Charges__c;
                    tempInvoiceItem.AFZA_Other_Charges__c = feeObject2[0].AFZA_Other_Charges__c;
                    tempInvoiceItem.EMPost_Charges__c = feeObject2[0].EMPost_Charges__c;
                    tempInvoiceItem.Local_Government_Charges__c = feeObject2[0].Local_Government_Charges__c;
                    
                    tempInvoiceItem.Maintenance__c = feeObject2[0].Maintenance__c;
                    tempInvoiceItem.Establishment_Card__c = feeObject2[0].Establishment_Card__c;
                    tempInvoiceItem.Etisalat__c = feeObject2[0].Etisalat__c;
                    tempInvoiceItem.Google__c = feeObject2[0].Google__c;
                    tempInvoiceItem.Gov_Establishment_Card__c = feeObject2[0].Gov_Establishment_Card__c;
                    tempInvoiceItem.Sewage__c = feeObject2[0].Sewage__c;
                    tempInvoiceItem.Sign_Board__c = feeObject2[0].Sign_Board__c;
                    tempInvoiceItem.General_Services__c = feeObject2[0].General_Services__c;
                    
                    tempInvoiceItem.AFZA_Maintenance__c = feeObject2[0].AFZA_Maintenance__c;
                    tempInvoiceItem.AFZA_Establishment_Card__c = feeObject2[0].AFZA_Establishment_Card__c;
                    tempInvoiceItem.AFZA_Etisalat__c = feeObject2[0].AFZA_Etisalat__c;
                    tempInvoiceItem.AFZA_Google__c = feeObject2[0].AFZA_Google__c;
                    tempInvoiceItem.AFZA_Sewage__c = feeObject2[0].AFZA_Sewage__c;
                    tempInvoiceItem.AFZA_Sign_Board__c = feeObject2[0].AFZA_Sign_Board__c;
                    tempInvoiceItem.AFZA_General_Services__c = feeObject2[0].AFZA_General_Services__c;
                    
                    tempInvoiceItem.Advertisement_Fee__c = feeObject2[0].Advertisement_Fee__c;
                    tempInvoiceItem.AFZA_Advertisement_Fee__c = feeObject2[0].AFZA_Advertisement_Fee__c;
                    tempInvoiceItem.License_Activity_Fee__c = feeObject2[0].License_Activity_Fee__c;
                    tempInvoiceItem.AFZA_License_Activity_Fee__c = feeObject2[0].AFZA_License_Activity_Fee__c;
                    
                    
                    tempInvoiceItem.Employment_ID_Card__c = feeObject2[0].Employment_ID_Card__c;
                    tempInvoiceItem.AFZA_Employment_ID_Card__c = feeObject2[0].AFZA_Employment_ID_Card__c;
                    
                    //Check if Amount is Negative value or not...
                    if(tempInvoiceItem.Amount__c < 0)
                        tempInvoiceItem.Amount__c = 0;
                        
                    tempInvoiceItem.License_Delay_Fee__c = tempInvoiceItem.Amount__c;
                    tempInvoiceItem.AFZA_License_Delay_Fee__c = tempInvoiceItem.Amount__c;
                     
                    //Adding in the Location...
                    invoiceLineItemList.add(tempInvoiceItem);
                }
                
            }
        
            //From Suspension Invoice Items....
            if(suspensionInvoiceItemList.size() > 0)
            {
                for(Fines__c suspensionObject : suspensionInvoiceItemList)
                {
                    //We only neeed to put the Invoice Line Item...
                    tempInvoiceItem = new Invoice_Line_Item__c();
                    
                    //Mapping the Fields...
                    tempInvoiceItem.Name = suspensionObject.Name;
                    //tempInvoiceItem.Fee__c = servicePriceBookObject.Fee__r.Id;
                    //tempInvoiceItem.Fee_Type__c = servicePriceBookObject.Fee__r.Fee_Type__c;
                    tempInvoiceItem.Amount__c = suspensionObject.Fine_Total_Amount_Due__c;
                    tempInvoiceItem.Suspension__c = suspensionObject.Id;
                    
                    //Now Add the Invoice Item...
                    invoiceLineItemList.add(tempInvoiceItem);
                    
                }
            }
            
            //For Service Price Book....
            If(servicePriceBookList.size() > 0)
            {
                for(Service_Price_Book__c servicePriceBookObject : servicePriceBookList )
                {
                    //We only neeed to put the Invoice Line Item...
                    tempInvoiceItem = new Invoice_Line_Item__c();
                    
                    //Mapping the Fields...
                    tempInvoiceItem.Name = servicePriceBookObject.Fee__r.Name;
                    tempInvoiceItem.Fee__c = servicePriceBookObject.Fee__r.Id;
                    tempInvoiceItem.Fee_Type__c = servicePriceBookObject.Fee__r.Fee_Type__c;
                    tempInvoiceItem.Amount__c = servicePriceBookObject.Fee__r.Price__c;
                    tempInvoiceItem.Service_Price_Book__c = servicePriceBookObject.Id;
                    
                    tempInvoiceItem.AFZA_Charges__c = servicePriceBookObject.AFZA_Charges__c;
                    tempInvoiceItem.AFZA_Other_Charges__c = servicePriceBookObject.AFZA_Other_Charges__c;
                    tempInvoiceItem.EMPost_Charges__c = servicePriceBookObject.EMPost_Charges__c;
                    tempInvoiceItem.Local_Government_Charges__c = servicePriceBookObject.Local_Government_Charges__c;
                    
                    tempInvoiceItem.Maintenance__c = servicePriceBookObject.Maintenance__c;
                    tempInvoiceItem.Establishment_Card__c = servicePriceBookObject.Establishment_Card__c;
                    tempInvoiceItem.Etisalat__c = servicePriceBookObject.Etisalat__c;
                    tempInvoiceItem.Google__c = servicePriceBookObject.Google__c;
                    tempInvoiceItem.Gov_Establishment_Card__c = servicePriceBookObject.Gov_Establishment_Card__c;
                    tempInvoiceItem.Sewage__c = servicePriceBookObject.Sewage__c;
                    tempInvoiceItem.Sign_Board__c = servicePriceBookObject.Sign_Board__c;
                    tempInvoiceItem.General_Services__c = servicePriceBookObject.General_Services__c;
                    
                    tempInvoiceItem.AFZA_Maintenance__c = servicePriceBookObject.AFZA_Maintenance__c;
                    tempInvoiceItem.AFZA_Establishment_Card__c = servicePriceBookObject.AFZA_Establishment_Card__c;
                    tempInvoiceItem.AFZA_Etisalat__c = servicePriceBookObject.AFZA_Etisalat__c;
                    tempInvoiceItem.AFZA_Google__c = servicePriceBookObject.AFZA_Google__c;
                    tempInvoiceItem.AFZA_Sewage__c = servicePriceBookObject.AFZA_Sewage__c;
                    tempInvoiceItem.AFZA_Sign_Board__c = servicePriceBookObject.AFZA_Sign_Board__c;
                    tempInvoiceItem.AFZA_General_Services__c = servicePriceBookObject.AFZA_General_Services__c;
                                        
                    tempInvoiceItem.Advertisement_Fee__c = servicePriceBookObject.Advertisement_Fee__c;
                    tempInvoiceItem.AFZA_Advertisement_Fee__c = servicePriceBookObject.AFZA_Advertisement_Fee__c;
                    tempInvoiceItem.License_Activity_Fee__c = servicePriceBookObject.License_Activity_Fee__c;
                    tempInvoiceItem.AFZA_License_Activity_Fee__c = servicePriceBookObject.AFZA_License_Activity_Fee__c;
                    
                    tempInvoiceItem.Employment_ID_Card__c = servicePriceBookObject.Employment_ID_Card__c;
                    tempInvoiceItem.AFZA_Employment_ID_Card__c = servicePriceBookObject.AFZA_Employment_ID_Card__c;
                    
                    //Now Add the Invoice Item...
                    invoiceLineItemList.add(tempInvoiceItem);
                    
                }
            
            }
            
            //For Location Booking Invoice and Invoice Line Item..
            if(locationBookingList.size() > 0)
            {
                for(Tenancy_Contract__c bookingObject : locationBookingList)
                {
                    System.debug('The Start Date => '+bookingObject.Tenancy_Contract_Expiry_Date__c );
                    /*//Now Create a Invoice Line Item for this...
                    tempInvoiceItem = new Invoice_Line_Item__c();
                    //Mapping the Fields....
                    tempInvoiceItem.Name = bookingObject.Location__r.Fee__r.Name;
                    tempInvoiceItem.Fee__c = bookingObject.Location__r.Fee__r.Id;
                    tempInvoiceItem.Fee_Type__c = bookingObject.Location__r.Fee__r.Fee_Type__c;
                    
                    tempInvoiceItem.Tenancy_Contract__c = bookingObject.Id;*/
                    
                    
                    //For Fine Calculation...
                    if(bookingObject.Tenancy_Contract_Expiry_Date__c < System.today())
                    {
                        if(feeObject.size() > 0)
                        {
                            tempInvoiceItem = new Invoice_Line_Item__c();
                            //Mapping the Fields...
                            //tempInvoiceItem.Name = bookingObject.Location__r.Name + ' Tenancy Delay Fee';
                            tempInvoiceItem.Name = feeObject[0].FeeName__c;
                            //tempInvoiceItem.Fee__c = feeObject.Id;
                            tempInvoiceItem.Fee__c = feeObject[0].Fee__c;
                            tempInvoiceItem.Location__c = bookingObject.Location__c;
                            tempInvoiceItem.Tenancy_Contract__c = bookingObject.Id;
                            tempInvoiceItem.Service_Price_Book__c = feeObject[0].Id;
                            tempInvoiceItem.Fee_Type__c = feeObject[0].Fee_Type__c;
                            tempInvoiceItem.Start_Date__c = bookingObject.Tenancy_Contract_Start_Date__c;
                            tempInvoiceItem.End_Date__c = (bookingObject.Tenancy_Contract_Expiry_Date__c);
                            System.debug('The Tenancy Contract => '+(decimal.valueof(System.today().daysbetween(bookingObject.Tenancy_Contract_Expiry_Date__c))));
                            
                            tempInvoiceItem.AFZA_Charges__c = feeObject[0].AFZA_Charges__c;
                            tempInvoiceItem.AFZA_Other_Charges__c = feeObject[0].AFZA_Other_Charges__c;
                            tempInvoiceItem.EMPost_Charges__c = feeObject[0].EMPost_Charges__c;
                            tempInvoiceItem.Local_Government_Charges__c = feeObject[0].Local_Government_Charges__c;
                            
                            tempInvoiceItem.Maintenance__c = feeObject[0].Maintenance__c;
                            tempInvoiceItem.Establishment_Card__c = feeObject[0].Establishment_Card__c;
                            tempInvoiceItem.Etisalat__c = feeObject[0].Etisalat__c;
                            tempInvoiceItem.Google__c = feeObject[0].Google__c;
                            tempInvoiceItem.Gov_Establishment_Card__c = feeObject[0].Gov_Establishment_Card__c;
                            tempInvoiceItem.Sewage__c = feeObject[0].Sewage__c;
                            tempInvoiceItem.Sign_Board__c = feeObject[0].Sign_Board__c;
                            tempInvoiceItem.General_Services__c = feeObject[0].General_Services__c;
                            
                            tempInvoiceItem.AFZA_Maintenance__c = feeObject[0].AFZA_Maintenance__c;
                            tempInvoiceItem.AFZA_Establishment_Card__c = feeObject[0].AFZA_Establishment_Card__c;
                            tempInvoiceItem.AFZA_Etisalat__c = feeObject[0].AFZA_Etisalat__c;
                            tempInvoiceItem.AFZA_Google__c = feeObject[0].AFZA_Google__c;
                            tempInvoiceItem.AFZA_Sewage__c = feeObject[0].AFZA_Sewage__c;
                            tempInvoiceItem.AFZA_Sign_Board__c = feeObject[0].AFZA_Sign_Board__c;
                            tempInvoiceItem.AFZA_General_Services__c = feeObject[0].AFZA_General_Services__c;
                            
                            tempInvoiceItem.Advertisement_Fee__c = feeObject[0].Advertisement_Fee__c;
                            tempInvoiceItem.AFZA_Advertisement_Fee__c = feeObject[0].AFZA_Advertisement_Fee__c;
                            tempInvoiceItem.License_Activity_Fee__c = feeObject[0].License_Activity_Fee__c;
                            tempInvoiceItem.AFZA_License_Activity_Fee__c = feeObject[0].AFZA_License_Activity_Fee__c;
                            
                            
                            tempInvoiceItem.Employment_ID_Card__c = feeObject[0].Employment_ID_Card__c;
                            tempInvoiceItem.AFZA_Employment_ID_Card__c = feeObject[0].AFZA_Employment_ID_Card__c;
                            //tempInvoiceItem.Amount__c = feeObject.Price__c * (System.today().daysbetween(bookingObject.Tenancy_Contract_Expiry_Date__c)/30);
                            //tempInvoiceItem.Amount__c = (((bookingObject.Location__r.Rental_Rate__c / 360)* 0.2) * ((System.today().daysbetween(bookingObject.Tenancy_Contract_Expiry_Date__c))+ 1));
                            
                            
                            
                            Decimal daybetweenValue = (Decimal.valueof(bookingObject.Tenancy_Contract_Expiry_Date__c.adddays(1).daysbetween(System.today()))).round(system.roundingMode.CEILING);
                            //tempInvoiceItem.Amount__c = feeObject[0].Price__c * (daybetweenValue);
                            tempInvoiceItem.Amount__c = bookingObject.Fine_Per_Day__c * (daybetweenValue);
                            
                            //Previous Approach May 22nd 2012
                            /*Decimal daysBetween = (Decimal.valueof((bookingObject.Tenancy_Contract_Expiry_Date__c.daysbetween(System.today()))).setScale(3) + 1);
                            //tempInvoiceItem.Amount__c = (((bookingObject.Location__r.Rental_Rate__c / 360)* 0.2) * daysBetween);
                            tempInvoiceItem.Amount__c = (((bookingObject.Rental_Rate__c / 360)* 0.2) * daysBetween);*/
                            
                            //Check if Amount is Negative value or not...
                            if(tempInvoiceItem.Amount__c < 0)
                                tempInvoiceItem.Amount__c = 0;
                            
                               
                            //tempInvoiceItem.AFZA_Charges__c = tempInvoiceItem.Amount__c;
                            tempInvoiceItem.Tenancy_Contract_Delay_Fee__c = tempInvoiceItem.Amount__c;
                            tempInvoiceItem.AFZA_Tenancy_Contract_Delay_Fee__c = tempInvoiceItem.Amount__c;
                             
                            //Adding in the Location...
                            invoiceLineItemList.add(tempInvoiceItem);
                            
                            
                        }
                           
                    }
                    
                    //For Invoice Item Start/End Date and Amount Logic...
                    /*if(bookingObject.Account_Expiry_Date__c == bookingObject.Tenancy_Contract_Expiry_Date__c)
                    {
                        tempInvoiceItem.Start_Date__c = bookingObject.Tenancy_Contract_Expiry_Date__c;
                        tempInvoiceItem.End_Date__c = (bookingObject.Tenancy_Contract_Expiry_Date__c).addyears(1).adddays(-1);
                        tempInvoiceItem.Amount__c = bookingObject.Location__r.Rental_Rate__c;   
                    }
                    else if((bookingObject.Tenancy_Contract_Expiry_Date__c > System.Today()) && (bookingObject.Account_Today_Expiry_Date__c < 30))
                    {
                        tempInvoiceItem.Start_Date__c = System.today();
                        tempInvoiceItem.End_Date__c = (bookingObject.Account_Expiry_Date__c).addyears(1).adddays(-1);
                        //tempInvoiceItem.Amount__c = ((bookingObject.Location__r.Rental_Rate__c / 360) * (bookingObject.Account_Expiry_Date_Minus_Today__c) );
                        tempInvoiceItem.Amount__c = ((bookingObject.Location__r.Rental_Rate__c / 360) * ((bookingObject.Account_Expiry_Date__c).addyears(1)).daysbetween(System.today()) );
                        
                    }
                    else
                    {
                        tempInvoiceItem.Start_Date__c = System.today();
                        tempInvoiceItem.End_Date__c = (bookingObject.Account_Expiry_Date__c).addyears(1).adddays(-1);
                        //tempInvoiceItem.Amount__c = ((bookingObject.Location__r.Rental_Rate__c / 360) * (bookingObject.Account_Expiry_Date_Minus_Tenancy_Expiry__c) );
                        tempInvoiceItem.Amount__c = ((bookingObject.Location__r.Rental_Rate__c / 360) * (bookingObject.Account_Expiry_Date__c).addyears(1).daysbetween(bookingObject.Tenancy_Contract_Expiry_Date__c) );
                        
                    }*/
                                       
                    
                     //Check if Amount is Negative value or not...
                    //if(tempInvoiceItem.Amount__c < 0)
                        //tempInvoiceItem.Amount__c = 0;
                    
                    System.debug('The Booking Object Id => '+ bookingObject.Id);
                    
                    //Now Add the Invoice Item in List...
                    //invoiceLineItemList.add(tempInvoiceItem);
                    
                
                }
                
            }
            
            //Now create the Invoice records...
            if(invoiceObjectMap.size() > 0)
            {
                System.debug('The Invoice Size => '+ invoiceObjectMap.size());
                //First Insert the invoice...
                insert invoiceObjectMap.values();
                
                for(Invoice__c tempInvoice : invoiceObjectMap.values())
                {
                    //Putting in the Result List...
                    resultList.add(tempInvoice.Id);
                    
                    //Set the Invoice Id in the Invoice Line Item...
                    if(invoiceLineItemList.size()> 0)
                    {
                        for(Invoice_Line_Item__c invoiceItem : invoiceLineItemList)
                        {
                            //set the Invoice Id...
                            invoiceItem.Invoice__c = tempInvoice.Id;
                        }   
                        
                    }
                     
                }
                //Now Insert the Invoice Line Item...
                if(invoiceLineItemList.size() > 0)
                    insert invoiceLineItemList;
                
                
                if(isPerformaInvoice == 'False')
                {
                    //Change the status of Account to 'Invoice Issued'...
                    Account accountObject = [select Id, License_Application_Step__c from Account where Id =: accountId];
                    if(accountObject != null)
                    {
                        accountObject.License_Application_Step__c = 'Invoice Issued';
                        update accountObject;
                    }   
                }
                                            
                
            }
            
            //Returning the Value...
            return resultList;    
            
        }
        catch(System.Exception exp)
        {
            System.debug('The Exception is => '+ exp.getMessage() + ' && The Line Number => '+ exp.getLineNumber());
            Database.rollback(sp);
            return null;
        
        }
        
        
    
    }
    
    //For Generating the Invoice For the Renewal With Additional Features (Add Location and Change Location) Methods...
    //WebService static List<string> generatingInvoiceAndLineItemForRenewalLicenseAdditionalMethod(ID accountId, string licenseApplicationType, string ActivityType,string accountExpiryDate, string additionType)
    //WebService static List<string> generatingInvoiceAndLineItemForRenewalLicenseAdditionalMethod(ID accountId, string licenseApplicationType, string ActivityType,string accountExpiryDate, string additionType,List<Payment_Plan__c> paymentPlanList)
    WebService static List<string> generatingInvoiceAndLineItemForRenewalLicenseAdditionalMethod(ID accountId, string licenseApplicationType, string ActivityType,string accountExpiryDate, string additionType,string isPerformaInvoice,List<Payment_Plan__c> paymentPlanList)
    {
        Savepoint sp = Database.setSavepoint();
        //Query to the Service Price Book For Invoice and Invoice Line Item...
        List<Service_Price_Book__c> servicePriceBookList = new List<Service_Price_Book__c>([Select Renewal_Amendment_Fee__c,AFZA_Renewal_Amendment_Fee__c,Advertisement_Fee__c,AFZA_Advertisement_Fee__c,AFZA_Establishment_Card__c,AFZA_Etisalat__c,AFZA_General_Services__c,AFZA_Google__c,AFZA_License_Activity_Fee__c,License_Activity_Fee__c,AFZA_Maintenance__c,AFZA_Sign_Board__c,AFZA_Sewage__c,Employment_ID_Card__c,AFZA_Employment_ID_Card__c,General_Services__c, Establishment_Card__c, Etisalat__c, Google__c, Gov_Establishment_Card__c, Maintenance__c, Sewage__c, Sign_Board__c,AFZA_Charges__c, AFZA_Other_Charges__c,    EMPost_Charges__c, Local_Government_Charges__c,Name, License_Application_Type__c,License_App_Category__c, Id, Fee__r.Fee_ID__c, Fee__r.Fee_Type__c, Fee__r.Receipt_Group__c, Fee__r.Price__c, Fee__r.Name, Fee__c, Description__c From Service_Price_Book__c where  License_Application_Type__c =: licenseApplicationType AND License_App_Category__c =: ActivityType AND Fee_Type__c =: 'Non Refundable' AND isActive__c =: true]);
        //List<Tenancy_Contract__c> locationBookingList = new List<Tenancy_Contract__c>([Select Fine_Per_Day__c,Account_Expiry_Date_Minus_Tenancy_Expiry__c,Account_Expiry_Date_Minus_Today__c,Account_Today_Expiry_Date__c,Account_Expiry_Date__c,Location__r.Rental_Rate__c,Location__r.Fee__r.Name, Location__r.Fee__r.Fee_Type__c, Location__r.Name,Location__r.Location_Type__c,Location__r.Id, Location__c, Id, Tenancy_Contract_Expiry_Date__c,  Tenancy_Contract_Start_Date__c, Rental_Rate__c,Tenant__c From Tenancy_Contract__c where Tenant__c =: accountId AND (Tenancy_Contract_Cancellation_Date__c =: null)]);
        List<Fines__c> suspensionInvoiceItemList = new List<Fines__c>([Select Id,Account__c,Status__c, Name,Fines_Included__c, Fine_Total_Amount_Due__c From Fines__c where Fines_Included__c = true and Status__c = 'Approved' AND Account__c =: accountId]); 
        
        List<Service_Price_Book__c> feeObject = new List<Service_Price_Book__c>([Select locationServiceFlag__c,Renewal_Amendment_Fee__c,AFZA_Renewal_Amendment_Fee__c,Advertisement_Fee__c,AFZA_Advertisement_Fee__c,AFZA_Establishment_Card__c,AFZA_Etisalat__c,AFZA_General_Services__c,AFZA_Google__c,AFZA_License_Activity_Fee__c,License_Activity_Fee__c,AFZA_Maintenance__c,AFZA_Sign_Board__c,AFZA_Sewage__c,Employment_ID_Card__c,AFZA_Employment_ID_Card__c,General_Services__c, Establishment_Card__c, Etisalat__c, Google__c, Gov_Establishment_Card__c, Maintenance__c, Sewage__c, Sign_Board__c,AFZA_Charges__c, AFZA_Other_Charges__c,   EMPost_Charges__c, Local_Government_Charges__c,FeeName__c,Price__c, License_Application_Type__c, License_App_Category__c, Id, Fee__c, Fee_Type__c From Service_Price_Book__c where License_Application_Type__c =: 'Renewal Amendment' AND License_App_Category__c =: ActivityType AND Fee_Type__c =: 'Tenancy Delay Fee For Renewal' AND isActive__c =: true order by CreatedDate limit 1 ]);
        List<Service_Price_Book__c> servicePriceBookLocationRenewalList = new List<Service_Price_Book__c>([Select locationServiceFlag__c,Renewal_Amendment_Fee__c,AFZA_Renewal_Amendment_Fee__c,Advertisement_Fee__c,AFZA_Advertisement_Fee__c,AFZA_Establishment_Card__c,AFZA_Etisalat__c,AFZA_General_Services__c,AFZA_Google__c,AFZA_License_Activity_Fee__c,License_Activity_Fee__c,AFZA_Maintenance__c,AFZA_Sign_Board__c,AFZA_Sewage__c,Employment_ID_Card__c,AFZA_Employment_ID_Card__c,General_Services__c, Establishment_Card__c, Etisalat__c, Google__c, Gov_Establishment_Card__c, Maintenance__c, Sewage__c, Sign_Board__c,Name, AFZA_Charges__c, AFZA_Other_Charges__c,EMPost_Charges__c, Local_Government_Charges__c,Location_Type__c,FeeName__c,Price__c, License_Application_Type__c,Fee_Type__c,License_App_Category__c, Id, Fee__r.Fee_ID__c, Fee__r.Fee_Type__c, Fee__r.Receipt_Group__c, Fee__r.Price__c, Fee__r.Name, Fee__c, Description__c From Service_Price_Book__c where  License_Application_Type__c =: 'Renewal' AND License_App_Category__c =: 'Location' AND Fee_Type__c !=: 'Guarantee' AND isActive__c =: true]);
        List<Service_Price_Book__c> servicePriceBookLocationRenewalAdditionalList = new List<Service_Price_Book__c>([Select locationServiceFlag__c,Renewal_Amendment_Fee__c,AFZA_Renewal_Amendment_Fee__c,Advertisement_Fee__c,AFZA_Advertisement_Fee__c,AFZA_Establishment_Card__c,AFZA_Etisalat__c,AFZA_General_Services__c,AFZA_Google__c,AFZA_License_Activity_Fee__c,License_Activity_Fee__c,AFZA_Maintenance__c,AFZA_Sign_Board__c,AFZA_Sewage__c,Employment_ID_Card__c,AFZA_Employment_ID_Card__c,General_Services__c, Establishment_Card__c, Etisalat__c, Google__c, Gov_Establishment_Card__c, Maintenance__c, Sewage__c, Sign_Board__c,Name, AFZA_Charges__c, AFZA_Other_Charges__c,EMPost_Charges__c, Local_Government_Charges__c,Location_Type__c,FeeName__c,Price__c, License_Application_Type__c,Fee_Type__c,License_App_Category__c, Id, Fee__r.Fee_ID__c, Fee__r.Fee_Type__c, Fee__r.Receipt_Group__c, Fee__r.Price__c, Fee__r.Name, Fee__c, Description__c From Service_Price_Book__c where  License_Application_Type__c =: 'Renewal Amendment' AND License_App_Category__c =: 'Location' AND isActive__c =: true]);

        List<Service_Price_Book__c> locationAmendmentFeeObject = new List<Service_Price_Book__c>([Select locationServiceFlag__c,Renewal_Amendment_Fee__c,AFZA_Renewal_Amendment_Fee__c,Advertisement_Fee__c,AFZA_Advertisement_Fee__c,AFZA_Establishment_Card__c,AFZA_Etisalat__c,AFZA_General_Services__c,AFZA_Google__c,AFZA_License_Activity_Fee__c,License_Activity_Fee__c,AFZA_Maintenance__c,AFZA_Sign_Board__c,AFZA_Sewage__c,Employment_ID_Card__c,AFZA_Employment_ID_Card__c,General_Services__c, Establishment_Card__c, Etisalat__c, Google__c, Gov_Establishment_Card__c, Maintenance__c, Sewage__c, Sign_Board__c,AFZA_Charges__c, AFZA_Other_Charges__c,  EMPost_Charges__c, Local_Government_Charges__c,FeeName__c,Price__c, License_Application_Type__c, License_App_Category__c, Id, Fee__c, Fee_Type__c From Service_Price_Book__c where License_Application_Type__c =: 'Renewal Amendment' AND License_App_Category__c =: ActivityType AND Fee_Type__c=: 'Location Amendment Fee' AND isActive__c =: true order by CreatedDate limit 1]);

        //For Account Locations...
        List<Account_Location__c> accountLocationList = new List<Account_Location__c>([Select New_Location__r.Rental_Rate__c,New_Location__r.Location_Type__c,Account_Expiry_Date__c,renewAddLocationFlag__c,Previous_Tenancy_Contract__r.Location__r.Location_Type__c,Previous_Tenancy_Contract__r.Account_Today_Expiry_Date__c,Previous_Tenancy_Contract__r.Location__r.Rental_Rate__c,Previous_Tenancy_Contract__r.Account_Expiry_Date__c,isRenewed__c, Previous_Tenancy_Contract__r.Fine_Per_Day__c, Previous_Tenancy_Contract__r.Tenancy_Contract_Cancellation_Date__c, Previous_Tenancy_Contract__r.Location_type__c, Previous_Tenancy_Contract__r.Rental_Rate__c, Previous_Tenancy_Contract__r.Tenancy_Contract_Expiry_Date__c, Previous_Tenancy_Contract__r.Tenancy_Contract_Start_Date__c, Previous_Tenancy_Contract__r.Tenant__c, Previous_Tenancy_Contract__r.Location__c, Previous_Tenancy_Contract__r.Location__r.Current_Tenancy_Contract__c, Previous_Tenancy_Contract__r.Id, Previous_Tenancy_Contract__c, Previous_Location__c, New_Location__c, Name, Id, Flag__c, Edit_Flag__c, Delete_Flag__c, Add_Flag__c, Account__c From Account_Location__c where Account__c =: accountId AND Delete_Flag__c =: false AND isRenewed__c =: false]);
        
        Map<string,Invoice__c> invoiceObjectMap = new Map<string,Invoice__c>();
        Invoice_Line_Item__c tempInvoiceItem = null;
        List<Invoice_Line_Item__c> invoiceLineItemList = new List<Invoice_Line_Item__c>();
        list<String> resultList = new list<string>(); 
        //For Packages...
        Map<string,Payment_Plan__c> paymentPlanMap = new Map<string,Payment_Plan__c>();
        if(paymentPlanList == null)
            paymentPlanList = new List<Payment_Plan__c>();
        
        //For Guarantee Flag...
        Boolean guaranteeInvoiceFlag = false;
        
        try
        {
            //Create a Invoice Object....
            Invoice__c invoiceObject1 = new Invoice__c();
            invoiceObject1.Account__c = accountId;
            invoiceObject1.Date__c = System.Today();
            invoiceObject1.Invoice_Type__c = 'Non Refundable';
            //invoiceObject1.Fee_Application_Type__c = 'Renewal Additional';
            invoiceObject1.Fee_Application_Type__c = 'Renewal Amendment';
            if(isPerformaInvoice == 'True')
                invoiceObject1.isPerformaInvoice__c = true;
            
            //Adding in Map....
            invoiceObjectMap.put('Non Refundable',invoiceObject1);
            
            //Create a Invoice Object....
            Invoice__c invoiceObject2 = new Invoice__c();
            invoiceObject2.Account__c = accountId;
            invoiceObject2.Date__c = System.Today();
            invoiceObject2.Invoice_Type__c = 'Guarantee';
            //invoiceObject2.Fee_Application_Type__c = 'Renewal Additional';
            invoiceObject2.Fee_Application_Type__c = 'Renewal Amendment';
            
            if(isPerformaInvoice == 'True')
                invoiceObject2.isPerformaInvoice__c = true;
                
            //Adding in Map....
            invoiceObjectMap.put('Guarantee',invoiceObject2);
            
            System.debug('The Account Expiry Date in Renewal Additional => '+ accountExpiryDate);
            //For License Fine...
            if(Date.parse(accountExpiryDate) < System.Today())
            {
                //We only neeed to put the Invoice Line Item...
                List<Service_Price_Book__c> feeObject1 = new List<Service_Price_Book__c>([Select locationServiceFlag__c,Advertisement_Fee__c,AFZA_Advertisement_Fee__c,AFZA_Establishment_Card__c,AFZA_Etisalat__c,AFZA_General_Services__c,AFZA_Google__c,AFZA_License_Activity_Fee__c,License_Activity_Fee__c,AFZA_Maintenance__c,AFZA_Sign_Board__c,AFZA_Sewage__c,Employment_ID_Card__c,AFZA_Employment_ID_Card__c,General_Services__c, Establishment_Card__c, Etisalat__c, Google__c, Gov_Establishment_Card__c, Maintenance__c, Sewage__c, Sign_Board__c,AFZA_Charges__c, AFZA_Other_Charges__c,EMPost_Charges__c, Local_Government_Charges__c,FeeName__c,Price__c, License_Application_Type__c, License_App_Category__c, Id, Fee__c, Fee_Type__c From Service_Price_Book__c where License_Application_Type__c =: 'Renewal' AND License_App_Category__c =: ActivityType AND Fee_Type__c =: 'License Delay Fee For Renewal' AND isActive__c =: true order by CreatedDate limit 1 ]);
                
                if(feeObject1.size() > 0)
                {
                    tempInvoiceItem = new Invoice_Line_Item__c();
                    //Mapping the Fields...
                    tempInvoiceItem.Name = 'License Delay Fee';
                    tempInvoiceItem.Fee__c = feeObject1[0].Fee__c;
                    tempInvoiceItem.Service_Price_Book__c = feeObject1[0].Id;
                    tempInvoiceItem.Fee_Type__c = feeObject1[0].Fee_Type__c;
                    tempInvoiceItem.Start_Date__c =  Date.parse(accountExpiryDate);
                    tempInvoiceItem.End_Date__c = System.today();
                    tempInvoiceItem.AFZA_Charges__c = feeObject1[0].AFZA_Charges__c;
                    tempInvoiceItem.AFZA_Other_Charges__c = feeObject1[0].AFZA_Other_Charges__c;
                    tempInvoiceItem.EMPost_Charges__c = feeObject1[0].EMPost_Charges__c;
                    tempInvoiceItem.Local_Government_Charges__c = feeObject1[0].Local_Government_Charges__c;
                    
                    tempInvoiceItem.Maintenance__c = feeObject1[0].Maintenance__c;
                    tempInvoiceItem.Establishment_Card__c = feeObject1[0].Establishment_Card__c;
                    tempInvoiceItem.Etisalat__c = feeObject1[0].Etisalat__c;
                    tempInvoiceItem.Google__c = feeObject1[0].Google__c;
                    tempInvoiceItem.Gov_Establishment_Card__c = feeObject1[0].Gov_Establishment_Card__c;
                    tempInvoiceItem.Sewage__c = feeObject1[0].Sewage__c;
                    tempInvoiceItem.Sign_Board__c = feeObject1[0].Sign_Board__c;
                    tempInvoiceItem.General_Services__c = feeObject1[0].General_Services__c;
                    
                    tempInvoiceItem.AFZA_Maintenance__c = feeObject1[0].AFZA_Maintenance__c;
                    tempInvoiceItem.AFZA_Establishment_Card__c = feeObject1[0].AFZA_Establishment_Card__c;
                    tempInvoiceItem.AFZA_Etisalat__c = feeObject1[0].AFZA_Etisalat__c;
                    tempInvoiceItem.AFZA_Google__c = feeObject1[0].AFZA_Google__c;
                    tempInvoiceItem.AFZA_Sewage__c = feeObject1[0].AFZA_Sewage__c;
                    tempInvoiceItem.AFZA_Sign_Board__c = feeObject1[0].AFZA_Sign_Board__c;
                    tempInvoiceItem.AFZA_General_Services__c = feeObject1[0].AFZA_General_Services__c;
                    
                    tempInvoiceItem.Advertisement_Fee__c = feeObject1[0].Advertisement_Fee__c;
                    tempInvoiceItem.AFZA_Advertisement_Fee__c = feeObject1[0].AFZA_Advertisement_Fee__c;
                    tempInvoiceItem.License_Activity_Fee__c = feeObject1[0].License_Activity_Fee__c;
                    tempInvoiceItem.AFZA_License_Activity_Fee__c = feeObject1[0].AFZA_License_Activity_Fee__c;
                    
                    
                    tempInvoiceItem.Employment_ID_Card__c = feeObject1[0].Employment_ID_Card__c;
                    tempInvoiceItem.AFZA_Employment_ID_Card__c = feeObject1[0].AFZA_Employment_ID_Card__c;
                    
                    tempInvoiceItem.Start_Date__c = Date.parse(accountExpiryDate);
                    tempInvoiceItem.End_Date__c = System.today();
                    
                    Decimal daybetweenValue = (Decimal.valueof(Date.parse(accountExpiryDate).daysbetween(System.today()))/30).round(system.roundingMode.CEILING);
                    tempInvoiceItem.Amount__c = feeObject1[0].Price__c * (daybetweenValue);
                     
                    //if the Value is less than zero...
                    if(tempInvoiceItem.Amount__c < 0)
                        tempInvoiceItem.Amount__c = 0;
                    
                    //tempInvoiceItem.AFZA_Charges__c = tempInvoiceItem.Amount__c;
                    tempInvoiceItem.License_Delay_Fee__c = tempInvoiceItem.Amount__c;
                    tempInvoiceItem.AFZA_License_Delay_Fee__c = tempInvoiceItem.Amount__c;
                    
                    //Adding in the Location...
                    invoiceLineItemList.add(tempInvoiceItem);   
                    
                } 
                
            }
        
            //From Suspension Invoice Items....
            if(suspensionInvoiceItemList.size() > 0)
            {
                for(Fines__c suspensionObject : suspensionInvoiceItemList)
                {
                    //We only neeed to put the Invoice Line Item...
                    tempInvoiceItem = new Invoice_Line_Item__c();
                    
                    //Mapping the Fields...
                    tempInvoiceItem.Name = suspensionObject.Name;
                    tempInvoiceItem.Amount__c = suspensionObject.Fine_Total_Amount_Due__c;
                    tempInvoiceItem.Suspension__c = suspensionObject.Id;
                    
                    //Now Add the Invoice Item...
                    invoiceLineItemList.add(tempInvoiceItem);
                    
                }
            }
            
            //For Service Price Book....
            If(servicePriceBookList.size() > 0)
            {
                for(Service_Price_Book__c servicePriceBookObject : servicePriceBookList )
                {
                    //We only neeed to put the Invoice Line Item...
                    tempInvoiceItem = new Invoice_Line_Item__c();
                    
                    //Mapping the Fields...
                    tempInvoiceItem.Name = servicePriceBookObject.Fee__r.Name;
                    tempInvoiceItem.Fee__c = servicePriceBookObject.Fee__r.Id;
                    tempInvoiceItem.Fee_Type__c = servicePriceBookObject.Fee__r.Fee_Type__c;
                    tempInvoiceItem.Amount__c = servicePriceBookObject.Fee__r.Price__c;
                    tempInvoiceItem.Service_Price_Book__c = servicePriceBookObject.Id;
                    tempInvoiceItem.Start_Date__c = Date.parse(accountExpiryDate).adddays(1); 
                    tempInvoiceItem.End_Date__c = Date.parse(accountExpiryDate).addyears(1);
                    
                    tempInvoiceItem.AFZA_Charges__c = servicePriceBookObject.AFZA_Charges__c;
                    tempInvoiceItem.AFZA_Other_Charges__c = servicePriceBookObject.AFZA_Other_Charges__c;
                    tempInvoiceItem.EMPost_Charges__c = servicePriceBookObject.EMPost_Charges__c;
                    tempInvoiceItem.Local_Government_Charges__c = servicePriceBookObject.Local_Government_Charges__c;
                    
                    tempInvoiceItem.Maintenance__c = servicePriceBookObject.Maintenance__c;
                    tempInvoiceItem.Establishment_Card__c = servicePriceBookObject.Establishment_Card__c;
                    tempInvoiceItem.Etisalat__c = servicePriceBookObject.Etisalat__c;
                    tempInvoiceItem.Google__c = servicePriceBookObject.Google__c;
                    tempInvoiceItem.Gov_Establishment_Card__c = servicePriceBookObject.Gov_Establishment_Card__c;
                    tempInvoiceItem.Sewage__c = servicePriceBookObject.Sewage__c;
                    tempInvoiceItem.Sign_Board__c = servicePriceBookObject.Sign_Board__c;
                    tempInvoiceItem.General_Services__c = servicePriceBookObject.General_Services__c;
                    
                    tempInvoiceItem.AFZA_Maintenance__c = servicePriceBookObject.AFZA_Maintenance__c;
                    tempInvoiceItem.AFZA_Establishment_Card__c = servicePriceBookObject.AFZA_Establishment_Card__c;
                    tempInvoiceItem.AFZA_Etisalat__c = servicePriceBookObject.AFZA_Etisalat__c;
                    tempInvoiceItem.AFZA_Google__c = servicePriceBookObject.AFZA_Google__c;
                    tempInvoiceItem.AFZA_Sewage__c = servicePriceBookObject.AFZA_Sewage__c;
                    tempInvoiceItem.AFZA_Sign_Board__c = servicePriceBookObject.AFZA_Sign_Board__c;
                    tempInvoiceItem.AFZA_General_Services__c = servicePriceBookObject.AFZA_General_Services__c;
                                        
                    tempInvoiceItem.Advertisement_Fee__c = servicePriceBookObject.Advertisement_Fee__c;
                    tempInvoiceItem.AFZA_Advertisement_Fee__c = servicePriceBookObject.AFZA_Advertisement_Fee__c;
                    tempInvoiceItem.License_Activity_Fee__c = servicePriceBookObject.License_Activity_Fee__c;
                    tempInvoiceItem.AFZA_License_Activity_Fee__c = servicePriceBookObject.AFZA_License_Activity_Fee__c;
                    
                    tempInvoiceItem.Employment_ID_Card__c = servicePriceBookObject.Employment_ID_Card__c;
                    tempInvoiceItem.AFZA_Employment_ID_Card__c = servicePriceBookObject.AFZA_Employment_ID_Card__c;
                    //tempInvoiceItem.Amendment_Charges__c = servicePriceBookObject.Renewal_Amendment_Fee__c;
                    
                    //Now Add the Invoice Item...
                    invoiceLineItemList.add(tempInvoiceItem);
                    
                }
            
            }
            
            //Comment on 02/October/2012....
            //For Location Amendment Fee....
            if(locationAmendmentFeeObject.size() > 0)
            {
                //We only neeed to put the Invoice Line Item...
                tempInvoiceItem = new Invoice_Line_Item__c();
                //Mapping the Fields...
                //tempInvoiceItem.Name = locationAmendmentFeeObject[0].Name;
                tempInvoiceItem.Name = locationAmendmentFeeObject[0].FeeName__c;
                //tempInvoiceItem.Fee__c = locationAmendmentFeeObject[0].Id;
                tempInvoiceItem.Fee__c = locationAmendmentFeeObject[0].Fee__c;
                tempInvoiceItem.Service_Price_Book__c = locationAmendmentFeeObject[0].Id;
                tempInvoiceItem.Fee_Type__c = locationAmendmentFeeObject[0].Fee_Type__c;
                tempInvoiceItem.Amount__c = locationAmendmentFeeObject[0].Price__c;
                tempInvoiceItem.Amendment_Charges__c = locationAmendmentFeeObject[0].Price__c;
                tempInvoiceItem.AFZA_Amendment_Charges__c = locationAmendmentFeeObject[0].Price__c;
        
                tempInvoiceItem.AFZA_Charges__c = locationAmendmentFeeObject[0].AFZA_Charges__c;
                tempInvoiceItem.AFZA_Other_Charges__c = locationAmendmentFeeObject[0].AFZA_Other_Charges__c;
                tempInvoiceItem.EMPost_Charges__c = locationAmendmentFeeObject[0].EMPost_Charges__c;
                tempInvoiceItem.Local_Government_Charges__c = locationAmendmentFeeObject[0].Local_Government_Charges__c;
                
                tempInvoiceItem.Maintenance__c = locationAmendmentFeeObject[0].Maintenance__c;
                tempInvoiceItem.Establishment_Card__c = locationAmendmentFeeObject[0].Establishment_Card__c;
                tempInvoiceItem.Etisalat__c = locationAmendmentFeeObject[0].Etisalat__c;
                tempInvoiceItem.Google__c = locationAmendmentFeeObject[0].Google__c;
                tempInvoiceItem.Gov_Establishment_Card__c = locationAmendmentFeeObject[0].Gov_Establishment_Card__c;
                tempInvoiceItem.Sewage__c = locationAmendmentFeeObject[0].Sewage__c;
                tempInvoiceItem.Sign_Board__c = locationAmendmentFeeObject[0].Sign_Board__c;
                tempInvoiceItem.General_Services__c = locationAmendmentFeeObject[0].General_Services__c;
                
                tempInvoiceItem.AFZA_Maintenance__c = locationAmendmentFeeObject[0].AFZA_Maintenance__c;
                tempInvoiceItem.AFZA_Establishment_Card__c = locationAmendmentFeeObject[0].AFZA_Establishment_Card__c;
                tempInvoiceItem.AFZA_Etisalat__c = locationAmendmentFeeObject[0].AFZA_Etisalat__c;
                tempInvoiceItem.AFZA_Google__c = locationAmendmentFeeObject[0].AFZA_Google__c;
                tempInvoiceItem.AFZA_Sewage__c = locationAmendmentFeeObject[0].AFZA_Sewage__c;
                tempInvoiceItem.AFZA_Sign_Board__c = locationAmendmentFeeObject[0].AFZA_Sign_Board__c;
                tempInvoiceItem.AFZA_General_Services__c = locationAmendmentFeeObject[0].AFZA_General_Services__c;
                                    
                tempInvoiceItem.Advertisement_Fee__c = locationAmendmentFeeObject[0].Advertisement_Fee__c;
                tempInvoiceItem.AFZA_Advertisement_Fee__c = locationAmendmentFeeObject[0].AFZA_Advertisement_Fee__c;
                tempInvoiceItem.License_Activity_Fee__c = locationAmendmentFeeObject[0].License_Activity_Fee__c;
                tempInvoiceItem.AFZA_License_Activity_Fee__c = locationAmendmentFeeObject[0].AFZA_License_Activity_Fee__c;
                
                tempInvoiceItem.Employment_ID_Card__c = locationAmendmentFeeObject[0].Employment_ID_Card__c;
                tempInvoiceItem.AFZA_Employment_ID_Card__c = locationAmendmentFeeObject[0].AFZA_Employment_ID_Card__c;
                        
                //Now Add the Invoice Item...
                invoiceLineItemList.add(tempInvoiceItem);
            
            }
                    
            
            //For Location Booking Invoice and Invoice Line Item..
            if(accountLocationList.size() > 0)
            {
                //For Package Selection Adding in the Map...
                if(paymentPlanList.size() > 0)
                {
                    for(Payment_Plan__c paymentPlanObject : paymentPlanList)
                    {
                        //Adding in the Map...
                        paymentPlanMap.put(paymentPlanObject.Location_Type__c,paymentPlanObject);
                    }
                }
                
                for(Account_Location__c bookingObject : accountLocationList)
                {
                    //For Add Location (Change Location )....
                    if(bookingObject.renewAddLocationFlag__c != true && bookingObject.New_Location__c != null)
                    {
                        //Now Create a Invoice Line Item for this...
                        Invoice_Line_Item__c tempInvoiceItem2 = new Invoice_Line_Item__c();
                        //Mapping the Fields....
                        tempInvoiceItem2.Name = 'Location Rental Rate';
                        tempInvoiceItem2.Fee_Type__c = 'Non Refundable';
                        tempInvoiceItem2.Location__c = bookingObject.New_Location__c;
                        
                        
                        //We also need to add the Balance in the Change Location...
                        if(additionType == 'Renew With Change Location')
                        {
                            if(System.today() < bookingObject.Previous_Tenancy_Contract__r.Tenancy_Contract_Expiry_Date__c)
                            {
                                Decimal monthbetweenValue = Decimal.valueof((System.today()).monthsBetween(bookingObject.Previous_Tenancy_Contract__r.Tenancy_Contract_Expiry_Date__c)).setScale(2);
                                Decimal daysBetweenValue = Decimal.valueof((System.today()).daysBetween(bookingObject.Previous_Tenancy_Contract__r.Tenancy_Contract_Expiry_Date__c)).setScale(2); //- decimal.valueof(( date.daysInMonth(System.Today().year(), System.Today().month()) -  System.Today().day() ) );
                                Decimal i =  daysBetweenValue - decimal.valueof(( date.daysInMonth(System.Today().year(), System.Today().month()) -  System.Today().day() ) );
                                 
                                if(monthbetweenValue > 0)
                                {
                                    Invoice_Line_Item__c tempInvoiceItem3 = new Invoice_Line_Item__c();
                                    //Mapping the Fields...
                                    tempInvoiceItem3.Name = 'Tenancy Contract Balance Amount';
                                    tempInvoiceItem3.Tenancy_Contract__c = bookingObject.Previous_Tenancy_Contract__c;
                                    tempInvoiceItem3.Location__c = bookingObject.Previous_Tenancy_Contract__r.Location__c;
                                    //tempInvoiceItem3.Amount__c = (-1) * ((bookingObject.Previous_Tenancy_Contract__r.Location__r.Rental_Rate__c / 360) * (monthbetweenValue)).setScale(2); 
                                    tempInvoiceItem3.Amount__c = (-1) * ((bookingObject.Previous_Tenancy_Contract__r.Location__r.Rental_Rate__c / 360) * (i)).setScale(2);
                                    tempInvoiceItem3.Start_Date__c = bookingObject.Previous_Tenancy_Contract__r.Tenancy_Contract_Start_Date__c;
                                    tempInvoiceItem3.End_Date__c = bookingObject.Previous_Tenancy_Contract__r.Tenancy_Contract_Expiry_Date__c;
                                    tempInvoiceItem3.isBalanceAmount__c = true;
                                    
                                    
                                    //Adding in the Location...
                                    invoiceLineItemList.add(tempInvoiceItem3);
                                
                                }   
                            }
                                    
                        }
                        
                        Boolean locationPerDayFlag = false;
                        Decimal locationPerDayDayBetweenValue = 0;

                                    
                        if(System.today() == bookingObject.Account_Expiry_Date__c)
                        {
                            tempInvoiceItem2.Start_Date__c = bookingObject.Account_Expiry_Date__c;
                            tempInvoiceItem2.End_Date__c = bookingObject.Account_Expiry_Date__c.addyears(1);
                            tempInvoiceItem2.Amount__c = bookingObject.New_Location__r.Rental_Rate__c;
                        }
                        else if(System.today() > bookingObject.Account_Expiry_Date__c) //Comes after the License Expiry Date...
                        {
                            //For Location Per Day Flag...
                            locationPerDayFlag = true;
                            
                            if(System.today() > bookingObject.Account_Expiry_Date__c.addyears(1))
                            {
                                Integer yearDifference = System.Today().year() - bookingObject.Account_Expiry_Date__c.year(); 
                                yearDifference = yearDifference + 1;
                                tempInvoiceItem2.Start_Date__c = System.Today();
                                tempInvoiceItem2.End_Date__c = bookingObject.Account_Expiry_Date__c.addyears(yearDifference);
                                //tempInvoiceItem2.End_Date__c = System.Today().addyears(1);
                                
                                Decimal daybetweenValue = Decimal.valueof((System.today()).daysbetween(bookingObject.Account_Expiry_Date__c.addyears(yearDifference))).setScale(2);
                                tempInvoiceItem2.Amount__c = ((bookingObject.New_Location__r.Rental_Rate__c / 360) * (daybetweenValue)).setScale(2);
                                System.debug('Today above Expiry Year The Days Between Value for Location => '+bookingObject.New_Location__c + ' is =>'+ daybetweenValue);
                                locationPerDayDayBetweenValue = daybetweenValue;
                            }
                            else
                            {
                                tempInvoiceItem2.Start_Date__c = System.Today();
                                tempInvoiceItem2.End_Date__c = bookingObject.Account_Expiry_Date__c.addyears(1);
                                Decimal daybetweenValue = Decimal.valueof((System.today()).daysbetween(bookingObject.Account_Expiry_Date__c.addyears(1))).setScale(2);
                                //Decimal daybetweenValue = Decimal.valueof((bookingObject.Account_Expiry_Date__c).daysbetween(System.Today().addyears(1))).setScale(2);
                                tempInvoiceItem2.Amount__c = ((bookingObject.New_Location__r.Rental_Rate__c / 360) * (daybetweenValue)).setScale(2);    
                                //Copy the Day Between Values...
                                locationPerDayDayBetweenValue = daybetweenValue;
                                
                                System.debug('Today Above Expiry Year The Days Between Value for Location => '+bookingObject.New_Location__c + ' is =>'+ daybetweenValue);
                                
                            }
                            
                        }
                        else if(System.today() < bookingObject.Account_Expiry_Date__c) //Comes Before Expiry Date...
                        {
                            //For Location Per Day Flag...
                            locationPerDayFlag = true;
                                                        
                            tempInvoiceItem2.Start_Date__c = System.today();
                            tempInvoiceItem2.End_Date__c = bookingObject.Account_Expiry_Date__c.addyears(1);
                            Decimal daybetweenValue = Decimal.valueof((System.today()).daysbetween(bookingObject.Account_Expiry_Date__c)).setScale(2);
                            tempInvoiceItem2.Amount__c = ((bookingObject.New_Location__r.Rental_Rate__c.setScale(2)) + ( (bookingObject.New_Location__r.Rental_Rate__c / 360) * daybetweenValue)).setScale(2) ;
                            System.debug('Today below Expiry Year The Days Between Value for Location => '+bookingObject.New_Location__c + ' is =>'+ daybetweenValue);
                            //Copy the Day Between Values...
                            locationPerDayDayBetweenValue = Decimal.valueof((System.today()).daysbetween(bookingObject.Account_Expiry_Date__c.addyears(1))).setScale(2);
                        
                        }
                        
                        
                        //Package Selection...
                        if(paymentPlanMap.get(bookingObject.New_Location__r.Location_Type__c) != null)
                        {
                            tempInvoiceItem2.Name = 'Location Rental Rate '+ paymentPlanMap.get(bookingObject.New_Location__r.Location_Type__c).Installment__c + ' Installment';
                            tempInvoiceItem2.Location_Rental_Rate__c = tempInvoiceItem2.Amount__c + (tempInvoiceItem2.Amount__c * paymentPlanMap.get(bookingObject.New_Location__r.Location_Type__c).Sign_Multiplier__c * (paymentPlanMap.get(bookingObject.New_Location__r.Location_Type__c).Percent__c / 100)    );
                            tempInvoiceItem2.Payment_Plan__c = paymentPlanMap.get(bookingObject.New_Location__r.Location_Type__c).Id;
                            tempInvoiceItem2.Package_Value__c = tempInvoiceItem2.Amount__c * paymentPlanMap.get(bookingObject.New_Location__r.Location_Type__c).Sign_Multiplier__c * (paymentPlanMap.get(bookingObject.New_Location__r.Location_Type__c).Percent__c / 100);
                            tempInvoiceItem2.Installment__c = paymentPlanMap.get(bookingObject.New_Location__r.Location_Type__c).Installment__c;
                            tempInvoiceItem2.Action__c = paymentPlanMap.get(bookingObject.New_Location__r.Location_Type__c).Action__c;
                            
                            //The Amount...
                            tempInvoiceItem2.Amount__c = tempInvoiceItem2.Amount__c + (tempInvoiceItem2.Amount__c * paymentPlanMap.get(bookingObject.New_Location__r.Location_Type__c).Sign_Multiplier__c * (paymentPlanMap.get(bookingObject.New_Location__r.Location_Type__c).Percent__c / 100)    );
                        }
                       
                        //Check if Amount is Negative value or not...
                        if(tempInvoiceItem2.Amount__c < 0)
                            tempInvoiceItem2.Amount__c = 0;
                        
                        tempInvoiceItem2.Location_Rental_Rate__c = tempInvoiceItem2.Amount__c;
                        tempInvoiceItem2.AFZA_Location_Rental_Rate__c = tempInvoiceItem2.Amount__c;
                        //Adding in the Location...
                        invoiceLineItemList.add(tempInvoiceItem2);  
                        
                        if(additionType == 'Renew With Change Location')
                        {
                            //For Tenancy Contract Delay Fee... For Change Location Only...
                            //For Fine Calculation...
                            if(bookingObject.Previous_Tenancy_Contract__r.Tenancy_Contract_Expiry_Date__c < System.today())
                            {
                                if(feeObject.size() > 0)
                                {
                                    tempInvoiceItem = new Invoice_Line_Item__c();
                                    //Mapping the Fields...
                                    //tempInvoiceItem.Name = bookingObject.Location__r.Name + ' Tenancy Delay Fee';
                                    tempInvoiceItem.Name = feeObject[0].FeeName__c;
                                    tempInvoiceItem.Fee__c = feeObject[0].Fee__c;
                                    tempInvoiceItem.Service_Price_Book__c = feeObject[0].Id;
                                    tempInvoiceItem.Tenancy_Contract__c = bookingObject.Previous_Tenancy_Contract__c;
                                    tempInvoiceItem.Location__c = bookingObject.Previous_Tenancy_Contract__r.Location__c;
                                    tempInvoiceItem.Fee_Type__c = feeObject[0].Fee_Type__c;
                                    
                                    //tempInvoiceItem.Start_Date__c = bookingObject.Previous_Tenancy_Contract__r.Tenancy_Contract_Start_Date__c;
                                    //tempInvoiceItem.End_Date__c = bookingObject.Previous_Tenancy_Contract__r.Tenancy_Contract_Expiry_Date__c;
                                    tempInvoiceItem.Start_Date__c = bookingObject.Previous_Tenancy_Contract__r.Tenancy_Contract_Expiry_Date__c;
                                    tempInvoiceItem.End_Date__c = System.today();
                                    
                                    tempInvoiceItem.AFZA_Charges__c = feeObject[0].AFZA_Charges__c;
                                    tempInvoiceItem.AFZA_Other_Charges__c = feeObject[0].AFZA_Other_Charges__c;
                                    tempInvoiceItem.EMPost_Charges__c = feeObject[0].EMPost_Charges__c;
                                    tempInvoiceItem.Local_Government_Charges__c = feeObject[0].Local_Government_Charges__c;
                                    tempInvoiceItem.Maintenance__c = feeObject[0].Maintenance__c;
                                    tempInvoiceItem.Establishment_Card__c = feeObject[0].Establishment_Card__c;
                                    tempInvoiceItem.Etisalat__c = feeObject[0].Etisalat__c;
                                    tempInvoiceItem.Google__c = feeObject[0].Google__c;
                                    tempInvoiceItem.Gov_Establishment_Card__c = feeObject[0].Gov_Establishment_Card__c;
                                    tempInvoiceItem.Sewage__c = feeObject[0].Sewage__c;
                                    tempInvoiceItem.Sign_Board__c = feeObject[0].Sign_Board__c;
                                    tempInvoiceItem.General_Services__c = feeObject[0].General_Services__c;
                                    
                                    tempInvoiceItem.AFZA_Maintenance__c = feeObject[0].AFZA_Maintenance__c;
                                    tempInvoiceItem.AFZA_Establishment_Card__c = feeObject[0].AFZA_Establishment_Card__c;
                                    tempInvoiceItem.AFZA_Etisalat__c = feeObject[0].AFZA_Etisalat__c;
                                    tempInvoiceItem.AFZA_Google__c = feeObject[0].AFZA_Google__c;
                                    tempInvoiceItem.AFZA_Sewage__c = feeObject[0].AFZA_Sewage__c;
                                    tempInvoiceItem.AFZA_Sign_Board__c = feeObject[0].AFZA_Sign_Board__c;
                                    tempInvoiceItem.AFZA_General_Services__c = feeObject[0].AFZA_General_Services__c;
                                    
                                    tempInvoiceItem.Advertisement_Fee__c = feeObject[0].Advertisement_Fee__c;
                                    tempInvoiceItem.AFZA_Advertisement_Fee__c = feeObject[0].AFZA_Advertisement_Fee__c;
                                    tempInvoiceItem.License_Activity_Fee__c = feeObject[0].License_Activity_Fee__c;
                                    tempInvoiceItem.AFZA_License_Activity_Fee__c = feeObject[0].AFZA_License_Activity_Fee__c;
                                    
                                    
                                    tempInvoiceItem.Employment_ID_Card__c = feeObject[0].Employment_ID_Card__c;
                                    tempInvoiceItem.AFZA_Employment_ID_Card__c = feeObject[0].AFZA_Employment_ID_Card__c;
                                    
                                    
                                    
                                    Decimal daybetweenValue = (Decimal.valueof(bookingObject.Previous_Tenancy_Contract__r.Tenancy_Contract_Expiry_Date__c.daysbetween(System.today().adddays(1)))).round(system.roundingMode.CEILING);
                                    tempInvoiceItem.Amount__c = (bookingObject.Previous_Tenancy_Contract__r.Fine_Per_Day__c * (daybetweenValue)).round(system.roundingMode.CEILING);
                                    
                                    //Check if Amount is Negative value or not...
                                    if(tempInvoiceItem.Amount__c < 0)
                                        tempInvoiceItem.Amount__c = 0;
                                    
                                    
                                    tempInvoiceItem.Tenancy_Contract_Delay_Fee__c  = tempInvoiceItem.Amount__c;
                                    tempInvoiceItem.AFZA_Tenancy_Contract_Delay_Fee__c  = tempInvoiceItem.Amount__c;
                                    //tempInvoiceItem.AFZA_Charges__c  = tempInvoiceItem.Amount__c;
                                    //Adding in the Location...
                                    invoiceLineItemList.add(tempInvoiceItem);   
                                    
                                   
                                    
                                }
                                
                            }   
                        
                        }
                         
                        System.debug('The servicePriceBookLocationRenewalAdditionalList Size => '+ servicePriceBookLocationRenewalAdditionalList.size()); 
                        
                        //Also doing for the location... 
                        if(servicePriceBookLocationRenewalAdditionalList.size() > 0)
                        {
                            //For the Service Price Book....
                            for(Service_Price_Book__c tempServiceObject : servicePriceBookLocationRenewalAdditionalList)
                            {
                                if(tempServiceObject.Location_Type__c == bookingObject.New_Location__r.Location_Type__c)
                                {
                                    if(tempServiceObject.Fee_Type__c == 'Guarantee')
                                        guaranteeInvoiceFlag = true;
                                            
                                    //Now Create a Invoice Line Item for this...
                                    tempInvoiceItem = new Invoice_Line_Item__c();
                                    //Mapping the Fields....
                                    tempInvoiceItem.Name = tempServiceObject.FeeName__c;
                                    tempInvoiceItem.Fee__c = tempServiceObject.Fee__c;
                                    tempInvoiceItem.Service_Price_Book__c = tempServiceObject.Id;
                                    tempInvoiceItem.Location__c = bookingObject.New_Location__c;
                                    tempInvoiceItem.Fee_Type__c = tempServiceObject.Fee_Type__c;
                                    
                                    if(tempServiceObject.locationServiceFlag__c == 'False')
                                        tempInvoiceItem.Amount__c = tempServiceObject.Price__c;
                                    else
                                    {
                                        //For Location Per Day Flag...
                                        if(locationPerDayFlag == true)
                                        {
                                            tempInvoiceItem.Amount__c = ((tempServiceObject.Price__c / 360) * (locationPerDayDayBetweenValue)).round(system.roundingMode.CEILING);
                                            System.debug('The Days Between Value for Location => '+bookingObject.New_Location__c + ' is =>'+ locationPerDayDayBetweenValue);                            
                                        }
                                        else
                                        {
                                            tempInvoiceItem.Amount__c = tempServiceObject.Price__c;
                                        }
                                    }   
                                    
                                    
                                    
                                    tempInvoiceItem.AFZA_Charges__c = tempServiceObject.AFZA_Charges__c;
                                    tempInvoiceItem.AFZA_Other_Charges__c = tempServiceObject.AFZA_Other_Charges__c;
                                    tempInvoiceItem.EMPost_Charges__c = tempServiceObject.EMPost_Charges__c;
                                    tempInvoiceItem.Local_Government_Charges__c = tempServiceObject.Local_Government_Charges__c;
                                    
                                    tempInvoiceItem.Maintenance__c = tempServiceObject.Maintenance__c;
                                    tempInvoiceItem.Establishment_Card__c = tempServiceObject.Establishment_Card__c;
                                    tempInvoiceItem.Etisalat__c = tempServiceObject.Etisalat__c;
                                    tempInvoiceItem.Google__c = tempServiceObject.Google__c;
                                    tempInvoiceItem.Gov_Establishment_Card__c = tempServiceObject.Gov_Establishment_Card__c;
                                    tempInvoiceItem.Sewage__c = tempServiceObject.Sewage__c;
                                    tempInvoiceItem.Sign_Board__c = tempServiceObject.Sign_Board__c;
                                    tempInvoiceItem.General_Services__c = tempServiceObject.General_Services__c;
                                    
                                    tempInvoiceItem.AFZA_Maintenance__c = tempServiceObject.AFZA_Maintenance__c;
                                    tempInvoiceItem.AFZA_Establishment_Card__c = tempServiceObject.AFZA_Establishment_Card__c;
                                    tempInvoiceItem.AFZA_Etisalat__c = tempServiceObject.AFZA_Etisalat__c;
                                    tempInvoiceItem.AFZA_Google__c = tempServiceObject.AFZA_Google__c;
                                    tempInvoiceItem.AFZA_Sewage__c = tempServiceObject.AFZA_Sewage__c;
                                    tempInvoiceItem.AFZA_Sign_Board__c = tempServiceObject.AFZA_Sign_Board__c;
                                    tempInvoiceItem.AFZA_General_Services__c = tempServiceObject.AFZA_General_Services__c;
                                    
                                    tempInvoiceItem.Advertisement_Fee__c = tempServiceObject.Advertisement_Fee__c;
                                    tempInvoiceItem.AFZA_Advertisement_Fee__c = tempServiceObject.AFZA_Advertisement_Fee__c;
                                    tempInvoiceItem.License_Activity_Fee__c = tempServiceObject.License_Activity_Fee__c;
                                    tempInvoiceItem.AFZA_License_Activity_Fee__c = tempServiceObject.AFZA_License_Activity_Fee__c;
                                    
                                    tempInvoiceItem.Employment_ID_Card__c = tempServiceObject.Employment_ID_Card__c;
                                    tempInvoiceItem.AFZA_Employment_ID_Card__c = tempServiceObject.AFZA_Employment_ID_Card__c;
                                    
                                    
                                    //Now Add the Invoice Item in List...
                                    invoiceLineItemList.add(tempInvoiceItem);
                                }
                                
                                                
                            }
                            
                        }
                        
                    }
                    else //Means its already in the Tenancy Contract Object...
                    {
                        System.debug('The Previous Tenancy Contract Expiry Date => '+ bookingObject.Previous_Tenancy_Contract__r.Tenancy_Contract_Expiry_Date__c);
                        System.debug('The Previous Tenancy Contract Location => '+ bookingObject.Previous_Tenancy_Contract__r.Location__c);
                        
                        //For Fine Calculation...
                        if(bookingObject.Previous_Tenancy_Contract__r.Tenancy_Contract_Expiry_Date__c < System.today())
                        {
                            if(feeObject.size() > 0)
                            {
                                tempInvoiceItem = new Invoice_Line_Item__c();
                                //Mapping the Fields...
                                //tempInvoiceItem.Name = bookingObject.Location__r.Name + ' Tenancy Delay Fee';
                                tempInvoiceItem.Name = feeObject[0].FeeName__c;
                                tempInvoiceItem.Fee__c = feeObject[0].Fee__c;
                                tempInvoiceItem.Service_Price_Book__c = feeObject[0].Id;
                                tempInvoiceItem.Tenancy_Contract__c = bookingObject.Previous_Tenancy_Contract__c;
                                tempInvoiceItem.Location__c = bookingObject.Previous_Tenancy_Contract__r.Location__c;
                                tempInvoiceItem.Fee_Type__c = feeObject[0].Fee_Type__c;
                                
                                //tempInvoiceItem.Start_Date__c = bookingObject.Previous_Tenancy_Contract__r.Tenancy_Contract_Start_Date__c;
                                //tempInvoiceItem.End_Date__c = (bookingObject.Previous_Tenancy_Contract__r.Tenancy_Contract_Expiry_Date__c);
                                tempInvoiceItem.Start_Date__c = bookingObject.Previous_Tenancy_Contract__r.Tenancy_Contract_Expiry_Date__c;
                                tempInvoiceItem.End_Date__c = System.Today();
                                
                                tempInvoiceItem.AFZA_Charges__c = feeObject[0].AFZA_Charges__c;
                                tempInvoiceItem.AFZA_Other_Charges__c = feeObject[0].AFZA_Other_Charges__c;
                                tempInvoiceItem.EMPost_Charges__c = feeObject[0].EMPost_Charges__c;
                                tempInvoiceItem.Local_Government_Charges__c = feeObject[0].Local_Government_Charges__c;
                                
                                tempInvoiceItem.Maintenance__c = feeObject[0].Maintenance__c;
                                tempInvoiceItem.Establishment_Card__c = feeObject[0].Establishment_Card__c;
                                tempInvoiceItem.Etisalat__c = feeObject[0].Etisalat__c;
                                tempInvoiceItem.Google__c = feeObject[0].Google__c;
                                tempInvoiceItem.Gov_Establishment_Card__c = feeObject[0].Gov_Establishment_Card__c;
                                tempInvoiceItem.Sewage__c = feeObject[0].Sewage__c;
                                tempInvoiceItem.Sign_Board__c = feeObject[0].Sign_Board__c;
                                tempInvoiceItem.General_Services__c = feeObject[0].General_Services__c;
                                
                                tempInvoiceItem.AFZA_Maintenance__c = feeObject[0].AFZA_Maintenance__c;
                                tempInvoiceItem.AFZA_Establishment_Card__c = feeObject[0].AFZA_Establishment_Card__c;
                                tempInvoiceItem.AFZA_Etisalat__c = feeObject[0].AFZA_Etisalat__c;
                                tempInvoiceItem.AFZA_Google__c = feeObject[0].AFZA_Google__c;
                                tempInvoiceItem.AFZA_Sewage__c = feeObject[0].AFZA_Sewage__c;
                                tempInvoiceItem.AFZA_Sign_Board__c = feeObject[0].AFZA_Sign_Board__c;
                                tempInvoiceItem.AFZA_General_Services__c = feeObject[0].AFZA_General_Services__c;
                                
                                tempInvoiceItem.Advertisement_Fee__c = feeObject[0].Advertisement_Fee__c;
                                tempInvoiceItem.AFZA_Advertisement_Fee__c = feeObject[0].AFZA_Advertisement_Fee__c;
                                tempInvoiceItem.License_Activity_Fee__c = feeObject[0].License_Activity_Fee__c;
                                tempInvoiceItem.AFZA_License_Activity_Fee__c = feeObject[0].AFZA_License_Activity_Fee__c;
                                
                                
                                tempInvoiceItem.Employment_ID_Card__c = feeObject[0].Employment_ID_Card__c;
                                tempInvoiceItem.AFZA_Employment_ID_Card__c = feeObject[0].AFZA_Employment_ID_Card__c;
                                                                                                
                                Decimal daybetweenValue = (Decimal.valueof(bookingObject.Previous_Tenancy_Contract__r.Tenancy_Contract_Expiry_Date__c.daysbetween(System.today().adddays(1)))).round(system.roundingMode.CEILING);
                                tempInvoiceItem.Amount__c = (bookingObject.Previous_Tenancy_Contract__r.Fine_Per_Day__c * (daybetweenValue)).round(system.roundingMode.CEILING);
                                
                                //Check if Amount is Negative value or not...
                                if(tempInvoiceItem.Amount__c < 0)
                                    tempInvoiceItem.Amount__c = 0;
                                
                                tempInvoiceItem.Tenancy_Contract_Delay_Fee__c  = tempInvoiceItem.Amount__c;
                                tempInvoiceItem.AFZA_Tenancy_Contract_Delay_Fee__c  = tempInvoiceItem.Amount__c;
                                //tempInvoiceItem.AFZA_Charges__c  = tempInvoiceItem.Amount__c;
                                //Adding in the Location...
                                invoiceLineItemList.add(tempInvoiceItem);   
                                
                            }
                            
                        }
                        
                        
                        //For Tenancy Contract Invoice Line Items...
                        if(bookingObject.Previous_Tenancy_Contract__r.Location__r.Current_Tenancy_Contract__c != null && bookingObject.Previous_Tenancy_Contract__c == bookingObject.Previous_Tenancy_Contract__r.Location__r.Current_Tenancy_Contract__c)
                        {
                            if(bookingObject.Previous_Tenancy_Contract__r.Tenancy_Contract_Expiry_Date__c <= bookingObject.Previous_Tenancy_Contract__r.Account_Expiry_Date__c.addyears(1).adddays(-1))
                            {
                                System.debug('The Start Date => '+bookingObject.Previous_Tenancy_Contract__r.Tenancy_Contract_Expiry_Date__c );
                                //Now Create a Invoice Line Item for this...
                                Invoice_Line_Item__c tempInvoiceItem1 = new Invoice_Line_Item__c();
                                //Mapping the Fields....
                                tempInvoiceItem1.Name = 'Location Rental Rate';
                                tempInvoiceItem1.Fee_Type__c = 'Non Refundable';
                                tempInvoiceItem1.Location__c = bookingObject.Previous_Tenancy_Contract__r.Location__c;
                                tempInvoiceItem1.Tenancy_Contract__c = bookingObject.Previous_Tenancy_Contract__c;    
                                System.debug('The Fee Name => '+tempInvoiceItem1.Name);
                                
                                
                                Boolean locationPerDayFlag = false;
                                Decimal locationPerDayDayBetweenValue = 0;
                                
                                //For Invoice Item Start/End Date and Amount Logic...
                                if(bookingObject.Previous_Tenancy_Contract__r.Account_Expiry_Date__c == bookingObject.Previous_Tenancy_Contract__r.Tenancy_Contract_Expiry_Date__c)
                                {
                                    tempInvoiceItem1.Start_Date__c = bookingObject.Previous_Tenancy_Contract__r.Tenancy_Contract_Expiry_Date__c.adddays(1);
                                    tempInvoiceItem1.End_Date__c = (bookingObject.Previous_Tenancy_Contract__r.Tenancy_Contract_Expiry_Date__c).addyears(1);
                                    tempInvoiceItem1.Amount__c = bookingObject.Previous_Tenancy_Contract__r.Location__r.Rental_Rate__c;
                                }
                                
                                else if((bookingObject.Previous_Tenancy_Contract__r.Tenancy_Contract_Expiry_Date__c < System.Today()) && (bookingObject.Previous_Tenancy_Contract__r.Account_Today_Expiry_Date__c < 30))
                                {
                                    //For Location Per Day Flag...
                                    locationPerDayFlag = true;
                                    
                                    
                                    tempInvoiceItem1.Start_Date__c = bookingObject.Previous_Tenancy_Contract__r.Tenancy_Contract_Expiry_Date__c.adddays(1);
                                    tempInvoiceItem1.End_Date__c = (bookingObject.Previous_Tenancy_Contract__r.Account_Expiry_Date__c).addyears(1);
                                    Decimal daybetweenValue = Decimal.valueof((bookingObject.Previous_Tenancy_Contract__r.Tenancy_Contract_Expiry_Date__c).daysbetween(bookingObject.Previous_Tenancy_Contract__r.Account_Expiry_Date__c.addyears(1))).setScale(2);
                                    tempInvoiceItem1.Amount__c = ((bookingObject.Previous_Tenancy_Contract__r.Location__r.Rental_Rate__c / 360) * (daybetweenValue)).setScale(2);
                                    
                                    locationPerDayDayBetweenValue = daybetweenValue;
                                    
                                }
                                else
                                {
                                    //For Location Per Day Flag...
                                    locationPerDayFlag = true;
                                    
                                    tempInvoiceItem1.Start_Date__c = bookingObject.Previous_Tenancy_Contract__r.Tenancy_Contract_Expiry_Date__c.adddays(1);
                                    tempInvoiceItem1.End_Date__c = (bookingObject.Previous_Tenancy_Contract__r.Account_Expiry_Date__c).addyears(1);
                                    Decimal daybetweenValue = Decimal.valueof((bookingObject.Previous_Tenancy_Contract__r.Tenancy_Contract_Expiry_Date__c).daysbetween(bookingObject.Previous_Tenancy_Contract__r.Account_Expiry_Date__c.addyears(1))).setScale(2);
                                    System.debug('The Issue Is => '+ daybetweenValue);
                                    tempInvoiceItem1.Amount__c = ((bookingObject.Previous_Tenancy_Contract__r.Location__r.Rental_Rate__c / 360) * (daybetweenValue) ).setScale(2);
                                    
                                    locationPerDayDayBetweenValue = daybetweenValue;    
                                }
                                
                                //Package Selection...
                                if(paymentPlanMap.get(bookingObject.Previous_Tenancy_Contract__r.Location__r.Location_Type__c) != null)
                                {
                                    tempInvoiceItem1.Name = 'Location Rental Rate '+ paymentPlanMap.get(bookingObject.Previous_Tenancy_Contract__r.Location__r.Location_Type__c).Installment__c + ' Installments';
                                    tempInvoiceItem1.Location_Rental_Rate__c = tempInvoiceItem1.Amount__c + (tempInvoiceItem1.Amount__c * paymentPlanMap.get(bookingObject.Previous_Tenancy_Contract__r.Location__r.Location_Type__c).Sign_Multiplier__c * (paymentPlanMap.get(bookingObject.Previous_Tenancy_Contract__r.Location__r.Location_Type__c).Percent__c / 100)    );
                                    tempInvoiceItem1.Payment_Plan__c = paymentPlanMap.get(bookingObject.Previous_Tenancy_Contract__r.Location__r.Location_Type__c).Id;
                                    tempInvoiceItem1.Package_Value__c = tempInvoiceItem1.Amount__c * paymentPlanMap.get(bookingObject.Previous_Tenancy_Contract__r.Location__r.Location_Type__c).Sign_Multiplier__c * (paymentPlanMap.get(bookingObject.Previous_Tenancy_Contract__r.Location__r.Location_Type__c).Percent__c / 100);
                                    tempInvoiceItem1.Installment__c = paymentPlanMap.get(bookingObject.Previous_Tenancy_Contract__r.Location__r.Location_Type__c).Installment__c;
                                    tempInvoiceItem1.Action__c = paymentPlanMap.get(bookingObject.Previous_Tenancy_Contract__r.Location__r.Location_Type__c).Action__c;
                                    
                                    //The Amount...
                                    tempInvoiceItem1.Amount__c = tempInvoiceItem1.Amount__c + (tempInvoiceItem1.Amount__c * paymentPlanMap.get(bookingObject.Previous_Tenancy_Contract__r.Location__r.Location_Type__c).Sign_Multiplier__c * (paymentPlanMap.get(bookingObject.Previous_Tenancy_Contract__r.Location__r.Location_Type__c).Percent__c / 100)    );
                                }
                                
                                //Check if Amount is Negative value or not...
                                if(tempInvoiceItem1.Amount__c < 0)
                                    tempInvoiceItem1.Amount__c = 0;
                                    
                                System.debug('The Booking Object Id => '+ bookingObject.Id);
                                
                                tempInvoiceItem1.Location_Rental_Rate__c = tempInvoiceItem1.Amount__c;
                                tempInvoiceItem1.AFZA_Location_Rental_Rate__c = tempInvoiceItem1.Amount__c;
                                
                                //Now Add the Invoice Item in List...
                                invoiceLineItemList.add(tempInvoiceItem1);
                                  
                                //Also doing for the location... 
                                if(servicePriceBookLocationRenewalList.size() > 0)
                                {
                                    //For the Service Price Book....
                                    for(Service_Price_Book__c tempServiceObject : servicePriceBookLocationRenewalList)
                                    {
                                        //Office, Warehouse, Accomodation,Land, E.O.C, Shop...
                                        if(tempServiceObject.Location_Type__c == bookingObject.Previous_Tenancy_Contract__r.Location__r.Location_Type__c)        
                                        {
                                            if(tempServiceObject.Fee_Type__c == 'Guarantee')
                                                guaranteeInvoiceFlag = true;
                                            
                                            //Now Create a Invoice Line Item for this...
                                            tempInvoiceItem = new Invoice_Line_Item__c();
                                            //Mapping the Fields....
                                            tempInvoiceItem.Name = tempServiceObject.FeeName__c;
                                            tempInvoiceItem.Fee__c = tempServiceObject.Fee__c;
                                            tempInvoiceItem.Service_Price_Book__c = tempServiceObject.Id;
                                            tempInvoiceItem.Tenancy_Contract__c = bookingObject.Previous_Tenancy_Contract__c;
                                            tempInvoiceItem.Location__c = bookingObject.Previous_Tenancy_Contract__r.Location__c;
                                            tempInvoiceItem.Fee_Type__c = tempServiceObject.Fee_Type__c;
                                            
                                            //Location Service Flag...
                                            if(tempServiceObject.locationServiceFlag__c == 'False')
                                                tempInvoiceItem.Amount__c = tempServiceObject.Price__c;
                                            else
                                            {
                                                //For Location Per Day Flag...
                                                if(locationPerDayFlag == true)
                                                {
                                                    tempInvoiceItem.Amount__c = ((tempServiceObject.Price__c / 360) * (locationPerDayDayBetweenValue)).round(system.roundingMode.CEILING);
                                                    System.debug('The Days Between Value for Location => '+bookingObject.Previous_Tenancy_Contract__r.Location__c + ' is =>'+ locationPerDayDayBetweenValue);                           
                                                }   
                                                else
                                                {
                                                    tempInvoiceItem.Amount__c = tempServiceObject.Price__c;
                                                }
                                            }   
                                            
                                            
                                            tempInvoiceItem.AFZA_Charges__c = tempServiceObject.AFZA_Charges__c;
                                            tempInvoiceItem.AFZA_Other_Charges__c = tempServiceObject.AFZA_Other_Charges__c;
                                            tempInvoiceItem.EMPost_Charges__c = tempServiceObject.EMPost_Charges__c;
                                            tempInvoiceItem.Local_Government_Charges__c = tempServiceObject.Local_Government_Charges__c;
                                            
                                            tempInvoiceItem.Maintenance__c = tempServiceObject.Maintenance__c;
                                            tempInvoiceItem.Establishment_Card__c = tempServiceObject.Establishment_Card__c;
                                            tempInvoiceItem.Etisalat__c = tempServiceObject.Etisalat__c;
                                            tempInvoiceItem.Google__c = tempServiceObject.Google__c;
                                            tempInvoiceItem.Gov_Establishment_Card__c = tempServiceObject.Gov_Establishment_Card__c;
                                            tempInvoiceItem.Sewage__c = tempServiceObject.Sewage__c;
                                            tempInvoiceItem.Sign_Board__c = tempServiceObject.Sign_Board__c;
                                            tempInvoiceItem.General_Services__c = tempServiceObject.General_Services__c;
                                            
                                            tempInvoiceItem.AFZA_Maintenance__c = tempServiceObject.AFZA_Maintenance__c;
                                            tempInvoiceItem.AFZA_Establishment_Card__c = tempServiceObject.AFZA_Establishment_Card__c;
                                            tempInvoiceItem.AFZA_Etisalat__c = tempServiceObject.AFZA_Etisalat__c;
                                            tempInvoiceItem.AFZA_Google__c = tempServiceObject.AFZA_Google__c;
                                            tempInvoiceItem.AFZA_Sewage__c = tempServiceObject.AFZA_Sewage__c;
                                            tempInvoiceItem.AFZA_Sign_Board__c = tempServiceObject.AFZA_Sign_Board__c;
                                            tempInvoiceItem.AFZA_General_Services__c = tempServiceObject.AFZA_General_Services__c;
                                            
                                            tempInvoiceItem.Advertisement_Fee__c = tempServiceObject.Advertisement_Fee__c;
                                            tempInvoiceItem.AFZA_Advertisement_Fee__c = tempServiceObject.AFZA_Advertisement_Fee__c;
                                            tempInvoiceItem.License_Activity_Fee__c = tempServiceObject.License_Activity_Fee__c;
                                            tempInvoiceItem.AFZA_License_Activity_Fee__c = tempServiceObject.AFZA_License_Activity_Fee__c;
                                            
                                            tempInvoiceItem.Employment_ID_Card__c = tempServiceObject.Employment_ID_Card__c;
                                            tempInvoiceItem.AFZA_Employment_ID_Card__c = tempServiceObject.AFZA_Employment_ID_Card__c;
                                            
                                            //Now Add the Invoice Item in List...
                                            invoiceLineItemList.add(tempInvoiceItem);
                                            
                                        }
                                    }
                                }
                            }   
                        }
                        
                    }
                    
                    
                }
                
            }
            
            
            //Now create the Invoice records...
            if(invoiceObjectMap.size() > 0)
            {
                
                //For Guarantee Invoice Remove if its not in placed...
                if(guaranteeInvoiceFlag != true)
                    invoiceObjectMap.remove('Guarantee');
                System.debug('The Invoice Object Map Size => '+ invoiceObjectMap.size());
                //First Insert the invoice...
                insert invoiceObjectMap.values();
                
                for(Invoice__c tempInvoiceObject : invoiceObjectMap.values())
                {
                    //Putting in the Result List...
                    resultList.add(tempInvoiceObject.Id);
                    
                    //Set the Invoice Id in the Invoice Line Item...
                    if(invoiceLineItemList.size()> 0)
                    {
                        for(Invoice_Line_Item__c invoiceItem : invoiceLineItemList)
                        {
                            if(tempInvoiceObject.Invoice_Type__c == 'Guarantee')
                            {
                                //set the Invoice Id...
                                if(invoiceItem.Fee_Type__c == 'Guarantee')
                                    invoiceItem.Invoice__c = tempInvoiceObject.Id;
                            }
                            else if(tempInvoiceObject.Invoice_Type__c == 'Non Refundable')
                            {
                                //set the Invoice Id...
                                if(invoiceItem.Fee_Type__c != 'Guarantee')
                                    invoiceItem.Invoice__c = tempInvoiceObject.Id;
                            }
                            
                            System.debug('The Invoice Item Fee Type => '+ invoiceItem.Fee_Type__c);
                            System.debug('The Invoice Item Invoice Value => '+ invoiceItem.Invoice__c);
                            
                        }   
                        
                    }
                        
                }
                
                
                //Now Insert the Invoice Line Item...
                if(invoiceLineItemList.size()> 0 )
                    insert invoiceLineItemList;
               
                if(isPerformaInvoice == 'False')
                {
                    //Change the status of Account to 'Invoice Issued'...
                    Account accountObject = [select Id, License_Application_Step__c from Account where Id =: accountId];
                    if(accountObject != null)
                    {
                        accountObject.License_Application_Step__c = 'Invoice Issued';
                        update accountObject;
                    }   
                }     
                
                       
                    
            }
            
            //return the values...
            return resultList; 
        
        }
        catch(System.Exception exp)
        {
            System.debug('The Exception is => '+ exp.getLineNumber() + ' & The Message' +exp.getMessage());
            Database.rollback(sp);
            return null;
        
        }
        
    }
    
    //For Generating the Invoice Button For Renewal License Methods New Approach (05/March/2012)....Company page Layouts....
    //WebService static List<string> generatingInvoiceAndLineItemForRenewalLicenseMethod(ID accountId, string licenseApplicationType, string ActivityType,string accountExpiryDate)
    //WebService static List<string> generatingInvoiceAndLineItemForRenewalLicenseMethod(ID accountId, string licenseApplicationType, string ActivityType,string accountExpiryDate, List<Payment_Plan__c> paymentPlanList)
    WebService static List<string> generatingInvoiceAndLineItemForRenewalLicenseMethod(ID accountId, string licenseApplicationType, string ActivityType,string accountExpiryDate, string isPerformaInvoice, List<Payment_Plan__c> paymentPlanList)
    {
        Savepoint sp = Database.setSavepoint();
        //Query to the Service Price Book For Invoice and Invoice Line Item...
        List<Service_Price_Book__c> servicePriceBookList = new List<Service_Price_Book__c>([Select Advertisement_Fee__c,AFZA_Advertisement_Fee__c,AFZA_Establishment_Card__c,AFZA_Etisalat__c,AFZA_General_Services__c,AFZA_Google__c,AFZA_License_Activity_Fee__c,License_Activity_Fee__c,AFZA_Maintenance__c,AFZA_Sign_Board__c,AFZA_Sewage__c,Employment_ID_Card__c,AFZA_Employment_ID_Card__c,Renewal_Amendment_Fee__c,AFZA_Renewal_Amendment_Fee__c,General_Services__c, Establishment_Card__c, Etisalat__c, Google__c, Gov_Establishment_Card__c, Maintenance__c, Sewage__c, Sign_Board__c,AFZA_Charges__c, AFZA_Other_Charges__c,    EMPost_Charges__c, Local_Government_Charges__c,Name, License_Application_Type__c,License_App_Category__c, Id, Fee__r.Fee_ID__c, Fee__r.Fee_Type__c, Fee__r.Receipt_Group__c, Fee__r.Price__c, Fee__r.Name, Fee__c, Description__c From Service_Price_Book__c where  License_Application_Type__c =: licenseApplicationType AND License_App_Category__c =: ActivityType AND Fee_Type__c =: 'Non Refundable' AND isActive__c =: true]);
        List<Tenancy_Contract__c> locationBookingList = new List<Tenancy_Contract__c>([Select Fine_Per_Day__c,Account_Expiry_Date_Minus_Tenancy_Expiry__c,Account_Expiry_Date_Minus_Today__c,Account_Today_Expiry_Date__c,Account_Expiry_Date__c,Location__r.Rental_Rate__c,Location__r.Fee__r.Name, Location__r.Fee__r.Fee_Type__c, Location__r.Name,Location__r.Location_Type__c,Location__r.Id, Location__c, Id, Tenancy_Contract_Expiry_Date__c,  Tenancy_Contract_Start_Date__c, Rental_Rate__c,Tenant__c From Tenancy_Contract__c where Tenant__c =: accountId AND (Tenancy_Contract_Cancellation_Date__c =: null)]);
        List<Fines__c> suspensionInvoiceItemList = new List<Fines__c>([Select Id,Account__c,Status__c, Name,Fines_Included__c, Fine_Total_Amount_Due__c From Fines__c where Fines_Included__c = true and Status__c = 'Approved' AND Account__c =: accountId]); 
        
        List<Service_Price_Book__c> feeObject = new List<Service_Price_Book__c>([Select locationServiceFlag__c,Advertisement_Fee__c,AFZA_Advertisement_Fee__c,AFZA_Establishment_Card__c,AFZA_Etisalat__c,AFZA_General_Services__c,AFZA_Google__c,AFZA_License_Activity_Fee__c,License_Activity_Fee__c,AFZA_Maintenance__c,AFZA_Sign_Board__c,AFZA_Sewage__c,Employment_ID_Card__c,AFZA_Employment_ID_Card__c,Renewal_Amendment_Fee__c,AFZA_Renewal_Amendment_Fee__c,General_Services__c, Establishment_Card__c, Etisalat__c, Google__c, Gov_Establishment_Card__c, Maintenance__c, Sewage__c, Sign_Board__c,AFZA_Charges__c, AFZA_Other_Charges__c,   EMPost_Charges__c, Local_Government_Charges__c,FeeName__c,Price__c, License_Application_Type__c, License_App_Category__c, Id, Fee__c, Fee_Type__c From Service_Price_Book__c where License_Application_Type__c =: 'Renewal' AND License_App_Category__c =: ActivityType AND Fee_Type__c =: 'Tenancy Delay Fee For Renewal' AND isActive__c =: true order by CreatedDate limit 1 ]);
        List<Service_Price_Book__c> servicePriceBookLocationList = new List<Service_Price_Book__c>([Select locationServiceFlag__c,Advertisement_Fee__c,AFZA_Advertisement_Fee__c,AFZA_Establishment_Card__c,AFZA_Etisalat__c,AFZA_General_Services__c,AFZA_Google__c,AFZA_License_Activity_Fee__c,License_Activity_Fee__c,AFZA_Maintenance__c,AFZA_Sign_Board__c,AFZA_Sewage__c,Employment_ID_Card__c,AFZA_Employment_ID_Card__c,Renewal_Amendment_Fee__c,AFZA_Renewal_Amendment_Fee__c,General_Services__c, Establishment_Card__c, Etisalat__c, Google__c, Gov_Establishment_Card__c, Maintenance__c, Sewage__c, Sign_Board__c,Name, AFZA_Charges__c, AFZA_Other_Charges__c,EMPost_Charges__c, Local_Government_Charges__c,Location_Type__c,FeeName__c,Price__c, License_Application_Type__c,Fee_Type__c,License_App_Category__c, Id, Fee__r.Fee_ID__c, Fee__r.Fee_Type__c, Fee__r.Receipt_Group__c, Fee__r.Price__c, Fee__r.Name, Fee__c, Description__c From Service_Price_Book__c where  License_Application_Type__c =: 'Renewal' AND License_App_Category__c =: 'Location' AND Fee_Type__c !=: 'Guarantee' AND isActive__c =: true]);
        
        Map<string,Invoice__c> invoiceObjectMap = new Map<string,Invoice__c>();
        Invoice_Line_Item__c tempInvoiceItem = null;
        List<Invoice_Line_Item__c> invoiceLineItemList = new List<Invoice_Line_Item__c>();
        list<String> resultList = new list<string>(); 
        //For Packages...
        Map<string,Payment_Plan__c> paymentPlanMap = new Map<string,Payment_Plan__c>();
        if(paymentPlanList == null)
            paymentPlanList = new List<Payment_Plan__c>();        
        
        try
        {
            //Create a Invoice Object....
            Invoice__c tempInvoiceObject = new Invoice__c();
            tempInvoiceObject.Account__c = accountId;
            tempInvoiceObject.Date__c = System.Today();
            tempInvoiceObject.Invoice_Type__c = 'Non Refundable';
            tempInvoiceObject.Fee_Application_Type__c = 'Renewal';
            
            if(isPerformaInvoice == 'True')
                tempInvoiceObject.isPerformaInvoice__c = true;
            //Adding in Map....
            invoiceObjectMap.put('Invoice',tempInvoiceObject);
            System.debug('The Invoice Object => '+ tempInvoiceObject + ' && The Account Expiry Date => '+ accountExpiryDate);
            System.debug('The Date Account Expiry Date => '+ accountExpiryDate);
            System.debug('The Date Account Expiry Date => '+ Date.parse(accountExpiryDate));
            
            //For License Fine...
            if(Date.parse(accountExpiryDate) < System.Today())
            {
                //We only neeed to put the Invoice Line Item...
                
                List<Service_Price_Book__c> feeObject1 = new List<Service_Price_Book__c>([Select Advertisement_Fee__c,AFZA_Advertisement_Fee__c,AFZA_Establishment_Card__c,AFZA_Etisalat__c,AFZA_General_Services__c,AFZA_Google__c,AFZA_License_Activity_Fee__c,License_Activity_Fee__c,AFZA_Maintenance__c,AFZA_Sign_Board__c,AFZA_Sewage__c,Employment_ID_Card__c,AFZA_Employment_ID_Card__c,Renewal_Amendment_Fee__c,AFZA_Renewal_Amendment_Fee__c,General_Services__c, Establishment_Card__c, Etisalat__c, Google__c, Gov_Establishment_Card__c, Maintenance__c, Sewage__c, Sign_Board__c,AFZA_Charges__c, AFZA_Other_Charges__c,EMPost_Charges__c, Local_Government_Charges__c,FeeName__c,Price__c, License_Application_Type__c, License_App_Category__c, Id, Fee__c, Fee_Type__c From Service_Price_Book__c where License_Application_Type__c =: 'Renewal' AND License_App_Category__c =: ActivityType AND Fee_Type__c =: 'License Delay Fee For Renewal' order by CreatedDate limit 1 ]);
                
                if(feeObject1.size() > 0)
                {
                    tempInvoiceItem = new Invoice_Line_Item__c();
                    //Mapping the Fields...
                    tempInvoiceItem.Name = 'License Delay Fee';
                    //tempInvoiceItem.Fee__c = feeObject1[0].Id;
                    tempInvoiceItem.Fee__c = feeObject1[0].Fee__c;
                    tempInvoiceItem.Service_Price_Book__c = feeObject1[0].Id;
                    tempInvoiceItem.Fee_Type__c = feeObject1[0].Fee_Type__c;
                    //tempInvoiceItem.Amount__c = feeObject1[0].Price__c * (System.today().daysbetween(Date.valueof(accountExpiryDate))/30);
                    
                    tempInvoiceItem.AFZA_Charges__c = feeObject1[0].AFZA_Charges__c;
                    tempInvoiceItem.AFZA_Other_Charges__c = feeObject1[0].AFZA_Other_Charges__c;
                    tempInvoiceItem.EMPost_Charges__c = feeObject1[0].EMPost_Charges__c;
                    tempInvoiceItem.Local_Government_Charges__c = feeObject1[0].Local_Government_Charges__c;
                    
                    tempInvoiceItem.Maintenance__c = feeObject1[0].Maintenance__c;
                    tempInvoiceItem.Establishment_Card__c = feeObject1[0].Establishment_Card__c;
                    tempInvoiceItem.Etisalat__c = feeObject1[0].Etisalat__c;
                    tempInvoiceItem.Google__c = feeObject1[0].Google__c;
                    tempInvoiceItem.Gov_Establishment_Card__c = feeObject1[0].Gov_Establishment_Card__c;
                    tempInvoiceItem.Sewage__c = feeObject1[0].Sewage__c;
                    tempInvoiceItem.Sign_Board__c = feeObject1[0].Sign_Board__c;
                    tempInvoiceItem.General_Services__c = feeObject1[0].General_Services__c;
                    
                    tempInvoiceItem.AFZA_Maintenance__c = feeObject1[0].AFZA_Maintenance__c;
                    tempInvoiceItem.AFZA_Establishment_Card__c = feeObject1[0].AFZA_Establishment_Card__c;
                    tempInvoiceItem.AFZA_Etisalat__c = feeObject1[0].AFZA_Etisalat__c;
                    tempInvoiceItem.AFZA_Google__c = feeObject1[0].AFZA_Google__c;
                    tempInvoiceItem.AFZA_Sewage__c = feeObject1[0].AFZA_Sewage__c;
                    tempInvoiceItem.AFZA_Sign_Board__c = feeObject1[0].AFZA_Sign_Board__c;
                    tempInvoiceItem.AFZA_General_Services__c = feeObject1[0].AFZA_General_Services__c;
                    
                    tempInvoiceItem.Advertisement_Fee__c = feeObject1[0].Advertisement_Fee__c;
                    tempInvoiceItem.AFZA_Advertisement_Fee__c = feeObject1[0].AFZA_Advertisement_Fee__c;
                    tempInvoiceItem.License_Activity_Fee__c = feeObject1[0].License_Activity_Fee__c;
                    tempInvoiceItem.AFZA_License_Activity_Fee__c = feeObject1[0].AFZA_License_Activity_Fee__c;
                    
                    tempInvoiceItem.Employment_ID_Card__c = feeObject1[0].Employment_ID_Card__c;
                    tempInvoiceItem.AFZA_Employment_ID_Card__c = feeObject1[0].AFZA_Employment_ID_Card__c;
                                        
                    tempInvoiceItem.Start_Date__c = Date.parse(accountExpiryDate);
                    tempInvoiceItem.End_Date__c = System.today();
                    
                    Decimal daybetweenValue = (Decimal.valueof(Date.parse(accountExpiryDate).daysbetween(System.today()))/30).round(system.roundingMode.CEILING);
                    tempInvoiceItem.Amount__c = feeObject1[0].Price__c * (daybetweenValue);
                     
                    //if the Value is less than zero...
                    if(tempInvoiceItem.Amount__c < 0)
                        tempInvoiceItem.Amount__c = 0;
                    
                    //tempInvoiceItem.AFZA_Charges__c = tempInvoiceItem.Amount__c;
                    tempInvoiceItem.License_Delay_Fee__c = tempInvoiceItem.Amount__c;
                    tempInvoiceItem.AFZA_License_Delay_Fee__c = tempInvoiceItem.Amount__c;
                    //Adding in the Location...
                    invoiceLineItemList.add(tempInvoiceItem);   
                    
                } 
                
                
                
            }
        
            //From Suspension Invoice Items....
            if(suspensionInvoiceItemList.size() > 0)
            {
                for(Fines__c suspensionObject : suspensionInvoiceItemList)
                {
                    //We only neeed to put the Invoice Line Item...
                    tempInvoiceItem = new Invoice_Line_Item__c();
                    
                    //Mapping the Fields...
                    tempInvoiceItem.Name = suspensionObject.Name;
                    //tempInvoiceItem.Fee__c = servicePriceBookObject.Fee__r.Id;
                    //tempInvoiceItem.Fee_Type__c = servicePriceBookObject.Fee__r.Fee_Type__c;
                    tempInvoiceItem.Amount__c = suspensionObject.Fine_Total_Amount_Due__c;
                    tempInvoiceItem.Suspension__c = suspensionObject.Id;
                    
                    //Now Add the Invoice Item...
                    invoiceLineItemList.add(tempInvoiceItem);
                    
                }
            }
            
            //For Service Price Book....
            If(servicePriceBookList.size() > 0)
            {
                for(Service_Price_Book__c servicePriceBookObject : servicePriceBookList )
                {
                    //We only neeed to put the Invoice Line Item...
                    tempInvoiceItem = new Invoice_Line_Item__c();
                    
                    //Mapping the Fields...
                    tempInvoiceItem.Name = servicePriceBookObject.Fee__r.Name;
                    tempInvoiceItem.Fee__c = servicePriceBookObject.Fee__r.Id;
                    tempInvoiceItem.Fee_Type__c = servicePriceBookObject.Fee__r.Fee_Type__c;
                    tempInvoiceItem.Amount__c = servicePriceBookObject.Fee__r.Price__c;
                    tempInvoiceItem.Service_Price_Book__c = servicePriceBookObject.Id;
                    tempInvoiceItem.Start_Date__c = System.today();
                    tempInvoiceItem.End_Date__c = Date.parse(accountExpiryDate).addYears(1);
                    
                    tempInvoiceItem.AFZA_Charges__c = servicePriceBookObject.AFZA_Charges__c;
                    tempInvoiceItem.AFZA_Other_Charges__c = servicePriceBookObject.AFZA_Other_Charges__c;
                    tempInvoiceItem.EMPost_Charges__c = servicePriceBookObject.EMPost_Charges__c;
                    tempInvoiceItem.Local_Government_Charges__c = servicePriceBookObject.Local_Government_Charges__c;
                    
                    tempInvoiceItem.Maintenance__c = servicePriceBookObject.Maintenance__c;
                    tempInvoiceItem.Establishment_Card__c = servicePriceBookObject.Establishment_Card__c;
                    tempInvoiceItem.Etisalat__c = servicePriceBookObject.Etisalat__c;
                    tempInvoiceItem.Google__c = servicePriceBookObject.Google__c;
                    tempInvoiceItem.Gov_Establishment_Card__c = servicePriceBookObject.Gov_Establishment_Card__c;
                    tempInvoiceItem.Sewage__c = servicePriceBookObject.Sewage__c;
                    tempInvoiceItem.Sign_Board__c = servicePriceBookObject.Sign_Board__c;
                    tempInvoiceItem.General_Services__c = servicePriceBookObject.General_Services__c;
                    
                    tempInvoiceItem.AFZA_Maintenance__c = servicePriceBookObject.AFZA_Maintenance__c;
                    tempInvoiceItem.AFZA_Establishment_Card__c = servicePriceBookObject.AFZA_Establishment_Card__c;
                    tempInvoiceItem.AFZA_Etisalat__c = servicePriceBookObject.AFZA_Etisalat__c;
                    tempInvoiceItem.AFZA_Google__c = servicePriceBookObject.AFZA_Google__c;
                    tempInvoiceItem.AFZA_Sewage__c = servicePriceBookObject.AFZA_Sewage__c;
                    tempInvoiceItem.AFZA_Sign_Board__c = servicePriceBookObject.AFZA_Sign_Board__c;
                    tempInvoiceItem.AFZA_General_Services__c = servicePriceBookObject.AFZA_General_Services__c;
                                        
                    tempInvoiceItem.Advertisement_Fee__c = servicePriceBookObject.Advertisement_Fee__c;
                    tempInvoiceItem.AFZA_Advertisement_Fee__c = servicePriceBookObject.AFZA_Advertisement_Fee__c;
                    tempInvoiceItem.License_Activity_Fee__c = servicePriceBookObject.License_Activity_Fee__c;
                    tempInvoiceItem.AFZA_License_Activity_Fee__c = servicePriceBookObject.AFZA_License_Activity_Fee__c;
                    
                    tempInvoiceItem.Employment_ID_Card__c = servicePriceBookObject.Employment_ID_Card__c;
                    tempInvoiceItem.AFZA_Employment_ID_Card__c = servicePriceBookObject.AFZA_Employment_ID_Card__c;
                    
                    //Now Add the Invoice Item...
                    invoiceLineItemList.add(tempInvoiceItem);
                    
                }
            
            }
            
            //For Location Booking Invoice and Invoice Line Item..
            if(locationBookingList.size() > 0)
            {
                //For Package Selection Adding in the Map...
                if(paymentPlanList.size() > 0)
                {
                    for(Payment_Plan__c paymentPlanObject : paymentPlanList)
                    {
                        //Adding in the Map...
                        paymentPlanMap.put(paymentPlanObject.Location_Type__c,paymentPlanObject);
                    }
                }
                
                for(Tenancy_Contract__c bookingObject : locationBookingList)
                {
                    
                    
                    
                    //For Fine Calculation...
                    if(bookingObject.Tenancy_Contract_Expiry_Date__c < System.today())
                    {
                        if(feeObject.size() > 0)
                        {
                            tempInvoiceItem = new Invoice_Line_Item__c();
                            //Mapping the Fields...
                            //tempInvoiceItem.Name = bookingObject.Location__r.Name + ' Tenancy Delay Fee';
                            tempInvoiceItem.Name = feeObject[0].FeeName__c;
                            //tempInvoiceItem.Fee__c = feeObject[0].Id;
                            tempInvoiceItem.Fee__c = feeObject[0].Fee__c;
                            tempInvoiceItem.Service_Price_Book__c = feeObject[0].Id;
                            tempInvoiceItem.Tenancy_Contract__c = bookingObject.Id;
                            tempInvoiceItem.Location__c = bookingObject.Location__c;
                            tempInvoiceItem.Fee_Type__c = feeObject[0].Fee_Type__c;
                            
                            tempInvoiceItem.Start_Date__c = bookingObject.Tenancy_Contract_Start_Date__c;
                            //tempInvoiceItem.End_Date__c = (bookingObject.Tenancy_Contract_Expiry_Date__c);
                            tempInvoiceItem.End_Date__c = System.today();
                            
                            tempInvoiceItem.AFZA_Charges__c = feeObject[0].AFZA_Charges__c;
                            tempInvoiceItem.AFZA_Other_Charges__c = feeObject[0].AFZA_Other_Charges__c;
                            tempInvoiceItem.EMPost_Charges__c = feeObject[0].EMPost_Charges__c;
                            tempInvoiceItem.Local_Government_Charges__c = feeObject[0].Local_Government_Charges__c;
                            
                            tempInvoiceItem.Maintenance__c = feeObject[0].Maintenance__c;
                            tempInvoiceItem.Establishment_Card__c = feeObject[0].Establishment_Card__c;
                            tempInvoiceItem.Etisalat__c = feeObject[0].Etisalat__c;
                            tempInvoiceItem.Google__c = feeObject[0].Google__c;
                            tempInvoiceItem.Gov_Establishment_Card__c = feeObject[0].Gov_Establishment_Card__c;
                            tempInvoiceItem.Sewage__c = feeObject[0].Sewage__c;
                            tempInvoiceItem.Sign_Board__c = feeObject[0].Sign_Board__c;
                            tempInvoiceItem.General_Services__c = feeObject[0].General_Services__c;
                            
                            
                            tempInvoiceItem.AFZA_Maintenance__c = feeObject[0].AFZA_Maintenance__c;
                            tempInvoiceItem.AFZA_Establishment_Card__c = feeObject[0].AFZA_Establishment_Card__c;
                            tempInvoiceItem.AFZA_Etisalat__c = feeObject[0].AFZA_Etisalat__c;
                            tempInvoiceItem.AFZA_Google__c = feeObject[0].AFZA_Google__c;
                            tempInvoiceItem.AFZA_Sewage__c = feeObject[0].AFZA_Sewage__c;
                            tempInvoiceItem.AFZA_Sign_Board__c = feeObject[0].AFZA_Sign_Board__c;
                            tempInvoiceItem.AFZA_General_Services__c = feeObject[0].AFZA_General_Services__c;
                                                
                            tempInvoiceItem.Advertisement_Fee__c = feeObject[0].Advertisement_Fee__c;
                            tempInvoiceItem.AFZA_Advertisement_Fee__c = feeObject[0].AFZA_Advertisement_Fee__c;
                            tempInvoiceItem.License_Activity_Fee__c = feeObject[0].License_Activity_Fee__c;
                            tempInvoiceItem.AFZA_License_Activity_Fee__c = feeObject[0].AFZA_License_Activity_Fee__c;
                            
                            tempInvoiceItem.Employment_ID_Card__c = feeObject[0].Employment_ID_Card__c;
                            tempInvoiceItem.AFZA_Employment_ID_Card__c = feeObject[0].AFZA_Employment_ID_Card__c;
                            
                                        
                            //tempInvoiceItem.Amount__c = feeObject.Price__c * (System.today().daysbetween(bookingObject.Tenancy_Contract_Expiry_Date__c)/30);
                            //tempInvoiceItem.Amount__c = (((bookingObject.Location__r.Rental_Rate__c / 360)* 0.2) * ((bookingObject.Tenancy_Contract_Expiry_Date__c.daysbetween(System.Today()))/30));
                            //Fine_Per_Day__c
                            Decimal daybetweenValue = (Decimal.valueof(bookingObject.Tenancy_Contract_Expiry_Date__c.daysbetween(System.today().adddays(1)))).round(system.roundingMode.CEILING);
                            //tempInvoiceItem.Amount__c = feeObject[0].Price__c * (daybetweenValue);
                            System.debug('The Days Between Value => '+ daybetweenValue);
                            System.debug('The Days Between Value with => '+ (Decimal.valueof(bookingObject.Tenancy_Contract_Expiry_Date__c.daysbetween(System.today().adddays(1)))).round(system.roundingMode.CEILING));
                            System.debug('The Fine Per Day => '+ bookingObject.Fine_Per_Day__c);
                            
                            tempInvoiceItem.Amount__c = (bookingObject.Fine_Per_Day__c * (daybetweenValue)).round(system.roundingMode.CEILING);
                            
                            
                            //Previous Approach before 22/May/2012
                            /*tempInvoiceItem.Start_Date__c = bookingObject.Tenancy_Contract_Start_Date__c;
                            tempInvoiceItem.End_Date__c = (bookingObject.Tenancy_Contract_Expiry_Date__c);
                            //tempInvoiceItem.Amount__c = feeObject.Price__c * (System.today().daysbetween(bookingObject.Tenancy_Contract_Expiry_Date__c)/30);
                            //tempInvoiceItem.Amount__c = (((bookingObject.Location__r.Rental_Rate__c / 360)* 0.2) * ((bookingObject.Tenancy_Contract_Expiry_Date__c.daysbetween(System.Today()))/30));
                            
                            Decimal daybetweenValue = (Decimal.valueof(bookingObject.Tenancy_Contract_Expiry_Date__c.daysbetween(System.today()))/30).round(system.roundingMode.CEILING);
                            tempInvoiceItem.Amount__c = feeObject[0].Price__c * (daybetweenValue);*/
                            
                            //Check if Amount is Negative value or not...
                            if(tempInvoiceItem.Amount__c < 0)
                                tempInvoiceItem.Amount__c = 0;
                            
                            
                            //tempInvoiceItem.AFZA_Charges__c  = tempInvoiceItem.Amount__c;
                            tempInvoiceItem.Tenancy_Contract_Delay_Fee__c  = tempInvoiceItem.Amount__c;
                            tempInvoiceItem.AFZA_Tenancy_Contract_Delay_Fee__c  = tempInvoiceItem.Amount__c;
                            //Adding in the Location...
                            invoiceLineItemList.add(tempInvoiceItem);   
                            
                        }
                        
                    }
                    
                    //For Tenancy Contract Invoice Line Items...
                    if(bookingObject.Tenancy_Contract_Expiry_Date__c <= bookingObject.Account_Expiry_Date__c.addyears(1).adddays(-1))
                    {
                        System.debug('The Start Date => '+bookingObject.Tenancy_Contract_Expiry_Date__c );
                        //Now Create a Invoice Line Item for this...
                        Invoice_Line_Item__c tempInvoiceItem1 = new Invoice_Line_Item__c();
                        //Mapping the Fields....
                        //tempInvoiceItem.Name = bookingObject.Location__r.Fee__r.Name;
                        //tempInvoiceItem1.Name = bookingObject.Location__r.Name + ' Rental Rate';
                        tempInvoiceItem1.Name = 'Location Rental Rate';
                        //tempInvoiceItem1.Fee__c = bookingObject.Location__r.Fee__r.Id;
                        //tempInvoiceItem1.Fee_Type__c = bookingObject.Location__r.Fee__r.Fee_Type__c;
                        tempInvoiceItem1.Fee_Type__c = 'Non Refundable';
                        tempInvoiceItem1.Location__c = bookingObject.Location__c;
                        tempInvoiceItem1.Tenancy_Contract__c = bookingObject.Id;    
                        System.debug('The Fee Name => '+tempInvoiceItem1.Name);
                        
                        Boolean locationPerDayFlag = false;
                        
                        //For Invoice Item Start/End Date and Amount Logic...
                        if(bookingObject.Account_Expiry_Date__c == bookingObject.Tenancy_Contract_Expiry_Date__c)
                        {
                            tempInvoiceItem1.Start_Date__c = bookingObject.Tenancy_Contract_Expiry_Date__c.adddays(1);
                            tempInvoiceItem1.End_Date__c = (bookingObject.Tenancy_Contract_Expiry_Date__c).addyears(1);
                            tempInvoiceItem1.Amount__c = bookingObject.Location__r.Rental_Rate__c;
                            //Previous Approach before 22/May/2012 
                            /*tempInvoiceItem1.Start_Date__c = bookingObject.Tenancy_Contract_Expiry_Date__c;
                            tempInvoiceItem1.End_Date__c = (bookingObject.Tenancy_Contract_Expiry_Date__c).addyears(1).adddays(-1);
                            tempInvoiceItem1.Amount__c = bookingObject.Location__r.Rental_Rate__c;*/   
                        }
                        
                        else if((bookingObject.Tenancy_Contract_Expiry_Date__c < System.Today()) && (bookingObject.Account_Today_Expiry_Date__c < 30))
                        {
                            //For Location Per Day Flag...
                            locationPerDayFlag = true;
                            
                            tempInvoiceItem1.Start_Date__c = bookingObject.Tenancy_Contract_Expiry_Date__c.adddays(1);
                            tempInvoiceItem1.End_Date__c = (bookingObject.Account_Expiry_Date__c).addyears(1);
                            //tempInvoiceItem1.Amount__c = ((bookingObject.Location__r.Rental_Rate__c / 360) * (bookingObject.Account_Expiry_Date_Minus_Today__c) );
                            //Decimal daybetweenValue = Decimal.valueof((bookingObject.Account_Expiry_Date__c).addyears(1).daysbetween(System.today() )).setScale(2);
                            Decimal daybetweenValue = Decimal.valueof((bookingObject.Tenancy_Contract_Expiry_Date__c).daysbetween(bookingObject.Account_Expiry_Date__c.addyears(1))).setScale(2);
                            //tempInvoiceItem1.Amount__c = ((bookingObject.Location__r.Rental_Rate__c / 360) * ((bookingObject.Account_Expiry_Date__c).addyears(1)).daysbetween(System.today()) );
                            tempInvoiceItem1.Amount__c = ((bookingObject.Location__r.Rental_Rate__c / 360) * (daybetweenValue)).setScale(2);
                            
                            System.debug('For Tenancy Contract => bookingObject.Tenancy_Contract_Expiry_Date__c > System.Today()) && (bookingObject.Account_Today_Expiry_Date__c < 30) => Days Between '+ daybetweenValue);
                            System.debug('For Tenancy Contract => bookingObject.Tenancy_Contract_Expiry_Date__c > System.Today()) && (bookingObject.Account_Today_Expiry_Date__c < 30) => Fee Price '+ (bookingObject.Location__r.Rental_Rate__c / 360));
                            
                            //Previous Approach 4/June/2012...
                            /*tempInvoiceItem1.Start_Date__c = System.today().adddays(1);
                            tempInvoiceItem1.End_Date__c = (bookingObject.Account_Expiry_Date__c).addyears(1);
                            //tempInvoiceItem1.Amount__c = ((bookingObject.Location__r.Rental_Rate__c / 360) * (bookingObject.Account_Expiry_Date_Minus_Today__c) );
                            //Decimal daybetweenValue = Decimal.valueof((bookingObject.Account_Expiry_Date__c).addyears(1).daysbetween(System.today() )).setScale(2);
                            Decimal daybetweenValue = Decimal.valueof((System.today()).daysbetween(bookingObject.Account_Expiry_Date__c.addyears(1))).setScale(2);
                            //tempInvoiceItem1.Amount__c = ((bookingObject.Location__r.Rental_Rate__c / 360) * ((bookingObject.Account_Expiry_Date__c).addyears(1)).daysbetween(System.today()) );
                            tempInvoiceItem1.Amount__c = ((bookingObject.Location__r.Rental_Rate__c / 360) * (daybetweenValue)).setScale(2);
                            
                            System.debug('For Tenancy Contract => bookingObject.Tenancy_Contract_Expiry_Date__c > System.Today()) && (bookingObject.Account_Today_Expiry_Date__c < 30) => Days Between '+ daybetweenValue);
                            System.debug('For Tenancy Contract => bookingObject.Tenancy_Contract_Expiry_Date__c > System.Today()) && (bookingObject.Account_Today_Expiry_Date__c < 30) => Fee Price '+ (bookingObject.Location__r.Rental_Rate__c / 360));*/
                            
                            //Previous Approach 31/May/2012...
                            /*tempInvoiceItem1.Start_Date__c = System.today().adddays(1);
                            tempInvoiceItem1.End_Date__c = (bookingObject.Account_Expiry_Date__c).addyears(1);
                            //tempInvoiceItem1.Amount__c = ((bookingObject.Location__r.Rental_Rate__c / 360) * (bookingObject.Account_Expiry_Date_Minus_Today__c) );
                            //Decimal daybetweenValue = Decimal.valueof((bookingObject.Account_Expiry_Date__c).addyears(1).daysbetween(System.today() )).setScale(2);
                            Decimal daybetweenValue = Decimal.valueof((System.today().adddays(1)).daysbetween(bookingObject.Account_Expiry_Date__c.addyears(1))).setScale(2);
                            //tempInvoiceItem1.Amount__c = ((bookingObject.Location__r.Rental_Rate__c / 360) * ((bookingObject.Account_Expiry_Date__c).addyears(1)).daysbetween(System.today()) );
                            tempInvoiceItem1.Amount__c = ((bookingObject.Location__r.Rental_Rate__c / 360) * (daybetweenValue)).setScale(2);
                            
                            System.debug('For Tenancy Contract => bookingObject.Tenancy_Contract_Expiry_Date__c > System.Today()) && (bookingObject.Account_Today_Expiry_Date__c < 30) => Days Between '+ daybetweenValue);
                            System.debug('For Tenancy Contract => bookingObject.Tenancy_Contract_Expiry_Date__c > System.Today()) && (bookingObject.Account_Today_Expiry_Date__c < 30) => Fee Price '+ (bookingObject.Location__r.Rental_Rate__c / 360));*/
                            
                            //Previous Approach before 22/May/2012
                            /*tempInvoiceItem1.Start_Date__c = System.today();
                            tempInvoiceItem1.End_Date__c = (bookingObject.Account_Expiry_Date__c).addyears(1).adddays(-1);
                            //tempInvoiceItem1.Amount__c = ((bookingObject.Location__r.Rental_Rate__c / 360) * (bookingObject.Account_Expiry_Date_Minus_Today__c) );
                            //Decimal daybetweenValue = Decimal.valueof((bookingObject.Account_Expiry_Date__c).addyears(1).daysbetween(System.today() )).setScale(2);
                            Decimal daybetweenValue = Decimal.valueof((System.today()).daysbetween(bookingObject.Account_Expiry_Date__c.addyears(1).adddays(-1) )).setScale(2);
                            //tempInvoiceItem1.Amount__c = ((bookingObject.Location__r.Rental_Rate__c / 360) * ((bookingObject.Account_Expiry_Date__c).addyears(1)).daysbetween(System.today()) );
                            tempInvoiceItem1.Amount__c = ((bookingObject.Location__r.Rental_Rate__c / 360) * (daybetweenValue)).setScale(2);
                            
                            System.debug('For Tenancy Contract => bookingObject.Tenancy_Contract_Expiry_Date__c > System.Today()) && (bookingObject.Account_Today_Expiry_Date__c < 30) => Days Between '+ daybetweenValue);
                            System.debug('For Tenancy Contract => bookingObject.Tenancy_Contract_Expiry_Date__c > System.Today()) && (bookingObject.Account_Today_Expiry_Date__c < 30) => Fee Price '+ (bookingObject.Location__r.Rental_Rate__c / 360));*/
                            
                        }
                        else
                        {
                            //For Location Per Day Flag...
                            locationPerDayFlag = true;
                            
                            tempInvoiceItem1.Start_Date__c = bookingObject.Tenancy_Contract_Expiry_Date__c.adddays(1);
                            tempInvoiceItem1.End_Date__c = (bookingObject.Account_Expiry_Date__c).addyears(1);
                            //tempInvoiceItem1.Amount__c = ((bookingObject.Location__r.Rental_Rate__c / 360) * (bookingObject.Account_Expiry_Date__c).addyears(1).daysbetween(bookingObject.Tenancy_Contract_Expiry_Date__c) );
                            //Decimal daybetweenValue = Decimal.valueof((bookingObject.Account_Expiry_Date__c).addyears(1).daysbetween(bookingObject.Tenancy_Contract_Expiry_Date__c)).setScale(2);
                            Decimal daybetweenValue = Decimal.valueof((bookingObject.Tenancy_Contract_Expiry_Date__c).daysbetween(bookingObject.Account_Expiry_Date__c.addyears(1))).setScale(2);
                            System.debug('The Issue Is => '+ daybetweenValue);
                            tempInvoiceItem1.Amount__c = ((bookingObject.Location__r.Rental_Rate__c / 360) * (daybetweenValue) ).setScale(2);
                            
                            System.debug('For Tenancy Contract => ELSE => Days Between '+ daybetweenValue);
                            System.debug('For Tenancy Contract => ESLE => '+ (bookingObject.Location__r.Rental_Rate__c / 360));
                            
                            //Previous Approach Before 31/May/2012
                            /*tempInvoiceItem1.Start_Date__c = bookingObject.Tenancy_Contract_Expiry_Date__c.adddays(1);
                            tempInvoiceItem1.End_Date__c = (bookingObject.Account_Expiry_Date__c).addyears(1);
                            //tempInvoiceItem1.Amount__c = ((bookingObject.Location__r.Rental_Rate__c / 360) * (bookingObject.Account_Expiry_Date__c).addyears(1).daysbetween(bookingObject.Tenancy_Contract_Expiry_Date__c) );
                            //Decimal daybetweenValue = Decimal.valueof((bookingObject.Account_Expiry_Date__c).addyears(1).daysbetween(bookingObject.Tenancy_Contract_Expiry_Date__c)).setScale(2);
                            Decimal daybetweenValue = Decimal.valueof((bookingObject.Tenancy_Contract_Expiry_Date__c.adddays(1)).daysbetween(bookingObject.Account_Expiry_Date__c.addyears(1))).setScale(2);
                            System.debug('The Issue Is => '+ daybetweenValue);
                            tempInvoiceItem1.Amount__c = ((bookingObject.Location__r.Rental_Rate__c / 360) * (daybetweenValue) ).setScale(2);
                            
                            System.debug('For Tenancy Contract => ELSE => Days Between '+ daybetweenValue);
                            System.debug('For Tenancy Contract => ESLE => '+ (bookingObject.Location__r.Rental_Rate__c / 360));*/
                            
                            //Previous Approach before 22/May/2012
                            /*
                            //tempInvoiceItem1.Start_Date__c = System.today();
                            tempInvoiceItem1.Start_Date__c = bookingObject.Tenancy_Contract_Expiry_Date__c;
                            tempInvoiceItem1.End_Date__c = (bookingObject.Account_Expiry_Date__c).addyears(1).adddays(-1);
                            //tempInvoiceItem1.Amount__c = ((bookingObject.Location__r.Rental_Rate__c / 360) * (bookingObject.Account_Expiry_Date__c).addyears(1).daysbetween(bookingObject.Tenancy_Contract_Expiry_Date__c) );
                            //Decimal daybetweenValue = Decimal.valueof((bookingObject.Account_Expiry_Date__c).addyears(1).daysbetween(bookingObject.Tenancy_Contract_Expiry_Date__c)).setScale(2);
                            Decimal daybetweenValue = Decimal.valueof((bookingObject.Tenancy_Contract_Expiry_Date__c).daysbetween(bookingObject.Account_Expiry_Date__c.addyears(1).adddays(-1))).setScale(2);
                            tempInvoiceItem1.Amount__c = ((bookingObject.Location__r.Rental_Rate__c / 360) * (daybetweenValue) ).setScale(2);
                            
                            System.debug('For Tenancy Contract => ELSE => Days Between '+ daybetweenValue);
                            System.debug('For Tenancy Contract => ESLE => '+ (bookingObject.Location__r.Rental_Rate__c / 360));*/
                            
                        }
                        
                        
                        
                        //Package Selection...
                        if(paymentPlanMap.get(bookingObject.Location__r.Location_Type__c) != null)
                        {
                            tempInvoiceItem1.Name = 'Location Rental Rate ' + paymentPlanMap.get(bookingObject.Location__r.Location_Type__c).Installment__c + ' Installments';
                            tempInvoiceItem1.Location_Rental_Rate__c = tempInvoiceItem1.Amount__c + (tempInvoiceItem1.Amount__c * paymentPlanMap.get(bookingObject.Location__r.Location_Type__c).Sign_Multiplier__c * (paymentPlanMap.get(bookingObject.Location__r.Location_Type__c).Percent__c / 100)    );
                            tempInvoiceItem1.Payment_Plan__c = paymentPlanMap.get(bookingObject.Location__r.Location_Type__c).Id;
                            tempInvoiceItem1.Package_Value__c = tempInvoiceItem1.Amount__c * paymentPlanMap.get(bookingObject.Location__r.Location_Type__c).Sign_Multiplier__c * (paymentPlanMap.get(bookingObject.Location__r.Location_Type__c).Percent__c / 100);
                            tempInvoiceItem1.Installment__c = paymentPlanMap.get(bookingObject.Location__r.Location_Type__c).Installment__c;
                            tempInvoiceItem1.Action__c = paymentPlanMap.get(bookingObject.Location__r.Location_Type__c).Action__c;
                            
                            //The Amount...
                            tempInvoiceItem1.Amount__c = tempInvoiceItem1.Amount__c + (tempInvoiceItem1.Amount__c * paymentPlanMap.get(bookingObject.Location__r.Location_Type__c).Sign_Multiplier__c * (paymentPlanMap.get(bookingObject.Location__r.Location_Type__c).Percent__c / 100)    );
                        
                        }
                       
                        
                        //Check if Amount is Negative value or not...
                        if(tempInvoiceItem1.Amount__c < 0)
                            tempInvoiceItem1.Amount__c = 0;
                            
                        System.debug('The Booking Object Id => '+ bookingObject.Id);
                        
                        tempInvoiceItem1.Location_Rental_Rate__c = tempInvoiceItem1.Amount__c;
                        tempInvoiceItem1.AFZA_Location_Rental_Rate__c = tempInvoiceItem1.Amount__c;
                        
                        //Now Add the Invoice Item in List...
                        invoiceLineItemList.add(tempInvoiceItem1);
                            
                        
                        //Also doing for the location... 
                        if(servicePriceBookLocationList.size() > 0)
                        {
                            //For the Service Price Book....
                            for(Service_Price_Book__c tempServiceObject : servicePriceBookLocationList)
                            {
                                //Office, Warehouse, Accomodation,Land, E.O.C, Shop...
                                if(tempServiceObject.Location_Type__c == bookingObject.Location__r.Location_Type__c)        
                                {
                                    //Now Create a Invoice Line Item for this...
                                    tempInvoiceItem = new Invoice_Line_Item__c();
                                    //Mapping the Fields....
                                    //tempInvoiceItem.Name = bookingObject.Location__r.Name + ' - '+ tempServiceObject.Location_Type__c +' Rental Rate';
                                    //tempInvoiceItem.Name = tempServiceObject.FeeName__c +' Rental Rate';
                                    tempInvoiceItem.Name = tempServiceObject.FeeName__c;
                                    tempInvoiceItem.Fee__c = tempServiceObject.Fee__c;
                                    tempInvoiceItem.Service_Price_Book__c = tempServiceObject.Id;
                                    tempInvoiceItem.Tenancy_Contract__c = bookingObject.Id;
                                    tempInvoiceItem.Location__c = bookingObject.Location__c;
                                    tempInvoiceItem.Fee_Type__c = tempServiceObject.Fee_Type__c;
                                    
                                    
                                    //Location Service Flag...
                                    if(tempServiceObject.locationServiceFlag__c == 'False')
                                        tempInvoiceItem.Amount__c = tempServiceObject.Price__c;
                                    else
                                    {
                                        tempInvoiceItem.Start_Date__c = bookingObject.Tenancy_Contract_Expiry_Date__c;
                                        tempInvoiceItem.End_Date__c = (bookingObject.Account_Expiry_Date__c).addyears(1);
                                        
                                        //For Location Per Day Flag...
                                        if(locationPerDayFlag == true)
                                        {
                                            Decimal daybetweenValue = Decimal.valueof((bookingObject.Tenancy_Contract_Expiry_Date__c).daysbetween(bookingObject.Account_Expiry_Date__c.addyears(1))).setScale(2);
                                            tempInvoiceItem.Amount__c = ((tempServiceObject.Price__c / 360) * (daybetweenValue)).round(system.roundingMode.CEILING);
                                            System.debug('The Days Between Value for Location => '+bookingObject.Location__r.name + ' is =>'+ daybetweenValue);                         
                                        }   
                                        else
                                        {
                                            tempInvoiceItem.Amount__c = tempServiceObject.Price__c;
                                        }
                                    
                                    }   
                                   
                                    //tempInvoiceItem.Start_Date__c = tempInvoiceItem1.Start_Date__c;
                                    //tempInvoiceItem.End_Date__c = tempInvoiceItem1.End_Date__c;
                                    
                                    tempInvoiceItem.AFZA_Charges__c = tempServiceObject.AFZA_Charges__c;
                                    tempInvoiceItem.AFZA_Other_Charges__c = tempServiceObject.AFZA_Other_Charges__c;
                                    tempInvoiceItem.EMPost_Charges__c = tempServiceObject.EMPost_Charges__c;
                                    tempInvoiceItem.Local_Government_Charges__c = tempServiceObject.Local_Government_Charges__c;
                                    
                                    tempInvoiceItem.Maintenance__c = tempServiceObject.Maintenance__c;
                                    tempInvoiceItem.Establishment_Card__c = tempServiceObject.Establishment_Card__c;
                                    tempInvoiceItem.Etisalat__c = tempServiceObject.Etisalat__c;
                                    tempInvoiceItem.Google__c = tempServiceObject.Google__c;
                                    tempInvoiceItem.Gov_Establishment_Card__c = tempServiceObject.Gov_Establishment_Card__c;
                                    tempInvoiceItem.Sewage__c = tempServiceObject.Sewage__c;
                                    tempInvoiceItem.Sign_Board__c = tempServiceObject.Sign_Board__c;
                                    tempInvoiceItem.General_Services__c = tempServiceObject.General_Services__c;
                                    
                                    tempInvoiceItem.AFZA_Maintenance__c = tempServiceObject.AFZA_Maintenance__c;
                                    tempInvoiceItem.AFZA_Establishment_Card__c = tempServiceObject.AFZA_Establishment_Card__c;
                                    tempInvoiceItem.AFZA_Etisalat__c = tempServiceObject.AFZA_Etisalat__c;
                                    tempInvoiceItem.AFZA_Google__c = tempServiceObject.AFZA_Google__c;
                                    tempInvoiceItem.AFZA_Sewage__c = tempServiceObject.AFZA_Sewage__c;
                                    tempInvoiceItem.AFZA_Sign_Board__c = tempServiceObject.AFZA_Sign_Board__c;
                                    tempInvoiceItem.AFZA_General_Services__c = tempServiceObject.AFZA_General_Services__c;
                                    
                                    tempInvoiceItem.Advertisement_Fee__c = tempServiceObject.Advertisement_Fee__c;
                                    tempInvoiceItem.AFZA_Advertisement_Fee__c = tempServiceObject.AFZA_Advertisement_Fee__c;
                                    tempInvoiceItem.License_Activity_Fee__c = tempServiceObject.License_Activity_Fee__c;
                                    tempInvoiceItem.AFZA_License_Activity_Fee__c = tempServiceObject.AFZA_License_Activity_Fee__c;
                                    
                                    tempInvoiceItem.Employment_ID_Card__c = tempServiceObject.Employment_ID_Card__c;
                                    tempInvoiceItem.AFZA_Employment_ID_Card__c = tempServiceObject.AFZA_Employment_ID_Card__c;
                                    
                                    
                                    //Now Add the Invoice Item in List...
                                    invoiceLineItemList.add(tempInvoiceItem);
                                    
                                }
                            }
                        }   
                    }
                    
                }
                
            }
            
            //Now create the Invoice records...
            if(invoiceObjectMap.size() > 0)
            {
                System.debug('The Invoice Size => '+ invoiceObjectMap.size());
                //First Insert the invoice...
                insert invoiceObjectMap.values();
                
                for(Invoice__c tempInvoice : invoiceObjectMap.values())
                {
                    //Putting in the Result List...
                    resultList.add(tempInvoice.Id);
                    
                    //Set the Invoice Id in the Invoice Line Item...
                    if(invoiceLineItemList.size()> 0)
                    {
                        for(Invoice_Line_Item__c invoiceItem : invoiceLineItemList)
                        {
                            //set the Invoice Id...
                            invoiceItem.Invoice__c = tempInvoice.Id;
                        }   
                        
                    }
                     
                }
                //Now Insert the Invoice Line Item...
                if(invoiceLineItemList.size() > 0)
                    insert invoiceLineItemList;
                
                
                if(isPerformaInvoice == 'False')
                {
                    //Change the status of Account to 'Invoice Issued'...
                    Account accountObject = [select Id, License_Application_Step__c from Account where Id =: accountId];
                    if(accountObject != null)
                    {
                        accountObject.License_Application_Step__c = 'Invoice Issued';
                        update accountObject;
                    }   
                }
                                        
            }
            
            //Returning the Value...
            return resultList;    
            
        }
        catch(System.Exception exp)
        {
            System.debug('The Exception is => '+ exp.getMessage());
            Database.rollback(sp);
            return null;
        
        }
        
    }
    
    //For Creating the Receipt Record Company License Number Assignment Method On Account Object Button...
    //WebService static string generatingReceiptForAccountMethod(ID accountId, List<ID> invoiceId,string companyLicenseNumber)
    WebService static List<string> generatingReceiptForAccountMethod(ID accountId, List<ID> invoiceId,string companyLicenseNumber)
    {
        Savepoint sp = Database.setSavepoint();
        try
        {
            
            //List<Invoice__c> invoiceObjectList = new List<Invoice__c>([Select Receipt_Generated__c,Total_Invoice_Formula_Field__c, Name, Invoice_Type__c, Id, Account__c,(Select Id,Name,Suspension__c from Invoice_Line_Items__r  where Suspension__c != null ) From Invoice__c where Id=: invoiceId AND Account__c =: accountId]);
            List<Invoice__c> invoiceObjectList = new List<Invoice__c>([Select Receipt_Generated__c,Total_Invoice_Formula_Field__c, Name, Invoice_Type__c, Id, Account__c,(Select Id,Name,Suspension__c from Invoice_Line_Items__r  where Suspension__c != null ) From Invoice__c where Id IN: invoiceId AND Account__c =: accountId]);
            List<Receipt__c> receiptList = new List<Receipt__c>();
            List<string> receiptIdList = new List<string>();
            
            RecordType receiptRecordType = new RecordType();
            receiptRecordType = [SELECT ID,DeveloperName,Name,SObjectType, IsActive FROM RecordType where DeveloperName =: 'Main_Receipt' AND SObjectType =: 'Receipt__c' AND IsActive =: true LIMIT 1];
            
            if(invoiceObjectList.size() > 0)
            {
                /*Receipt__c receiptObject = new Receipt__c();
                receiptObject.Company__c = accountId;
                receiptObject.Total_Amount_on_Invoice__c = invoiceObjectList[0].Total_Invoice_Formula_Field__c;
                receiptObject.Receipt_Type__c = invoiceObjectList[0].Invoice_Type__c;
                receiptObject.Related_Invoice__c = invoiceObjectList[0].Id;
                receiptObject.Date__c = System.Today();
                receiptObject.RecordTypeId = receiptRecordType.Id;
                //Now Adding in the Receipt List....
                insert receiptObject;*/
                
                
                //For Updating the Suspension Object first we need to Query the suspensions Id...
                List<ID> suspensionIdList = new List<ID>();
                
                for(Invoice__c invoiceObject : invoiceObjectList)
                {
                    Receipt__c receiptObject = new Receipt__c();
                    receiptObject.Company__c = accountId;
                    receiptObject.Total_Amount_on_Invoice__c = invoiceObject.Total_Invoice_Formula_Field__c;
                    receiptObject.Receipt_Type__c = invoiceObject.Invoice_Type__c;
                    receiptObject.Related_Invoice__c = invoiceObject.Id;
                    receiptObject.Date__c = System.Today();
                    receiptObject.RecordTypeId = receiptRecordType.Id;
                    //Now Adding in the Receipt List....
                    receiptList.add(receiptObject);
                    
                    for(Invoice_Line_Item__c invoiceItem : invoiceObject.Invoice_Line_Items__r)
                    {   
                        //Adding the Suspension Id...
                        suspensionIdList.add(invoiceItem.Suspension__c);    
                    }
                    
                    //Change the Invoice and Change the Status to Receipt Created...
                    invoiceObject.Receipt_Generated__c = true;
                }
                
                //Now Inserting the receiptObject...
                if(receiptList.size() > 0)
                    insert receiptList;
                
                //Now Update the Invoice...
                if(invoiceObjectList.size() > 0)
                    update invoiceObjectList;
                    
                //Now Query for the Suspension Object to update the record....
                if(suspensionIdList.size() > 0)
                {
                    List<Fines__c> suspensionUpdateList = new List<Fines__c>();
                    
                    for(Fines__c suspensionObject : [Select ID,Cleared_Date__c,Status__c, Name from Fines__c where Id =: suspensionIdList])
                    {
                        suspensionObject.Cleared_Date__c = system.today();
                        suspensionObject.Status__c = 'Completed';
                        
                        //Adding in the List...
                        suspensionUpdateList.add(suspensionObject); 
                    }
                    
                    //Now Update the List...
                    if(suspensionUpdateList.size() > 0)
                        update suspensionUpdateList;
                    
                    
                }
                
                List<Account> accountObject = new List<Account>([select Id,LicenseNumber__c,License_Application_Step__c from Account where Id =:accountId]);
                
                if(companyLicenseNumber == '' || companyLicenseNumber == null)
                {
                    System.debug('The Company License Id => '+ companyLicenseNumber);
                    List<License_Serial__c> licenseSerialObject = new List<License_Serial__c>([select Id,Name,License_Serial_Number__c, Status__c from License_Serial__c where Status__c = 'Available' ORDER BY License_Serial_Number__c ASC LIMIT 1]);
                        
                    if(licenseSerialObject.size() > 0)
                    {
                        if(accountObject.size() > 0)
                        {
                            System.debug('In Account Total => ');
                            accountObject[0].LicenseNumber__c = licenseSerialObject[0].Id;
                            accountObject[0].License_Application_Step__c = 'Receipt Issued';
                            //licenseSerialObject[0].Status__c = 'Reserved';
                            licenseSerialObject[0].Status__c = 'Leased';
                            
                            
                            System.debug('In Account Total => before update');
                            //Now Update the License Serial and Account ...
                            update accountObject;
                            update licenseSerialObject;
                            
                        }
                    }
                    else
                    {
                       if(accountObject.size() > 0)
                       {
                            System.debug('In Else Statement');
                           accountObject[0].License_Application_Step__c = 'Receipt Issued';
                           update accountObject;
                       }
                    }
                    
                }
                else
                {
                   if(accountObject.size() > 0)
                   {
                        System.debug('In Else Statement');
                       accountObject[0].License_Application_Step__c = 'Receipt Issued';
                       update accountObject;
                   }
                }
                
                //Returning the receipt Object Ids..
                //return receiptObject.Id;
                
                for(Receipt__c receiptObject : receiptList)
                {
                    receiptIdList.add(receiptObject.Id);
                }
                               
                return receiptIdList;
            }           
            
            //Else return null... 
            return null;
        }
        catch(System.Exception exp)
        {
            Database.rollback(sp);
            //return exp.getMessage();
            System.debug('The Exception is => '+ exp.getMessage());
            return null;
        
        }
    
    }
    
    //For Creating the Receipt Record Method On Service Letter Object Button...
    WebService static List<string> generatingReceiptForServiceLetterMethod(ID accountId, List<ID> invoiceId,ID serviceLetterId)
    {
        Savepoint sp = Database.setSavepoint();
        try
        {
            
            List<Invoice__c> invoiceObjectList = new List<Invoice__c>([Select Receipt_Generated__c,Total_Invoice_Formula_Field__c, Name, Invoice_Type__c, Id, Account__c,(Select Id,Name,Suspension__c from Invoice_Line_Items__r  where Suspension__c != null ) From Invoice__c where Id IN: invoiceId AND Account__c =: accountId]);
            List<Receipt__c> receiptList = new List<Receipt__c>();
            List<string> receiptIdsList = new List<string>();
            RecordType receiptRecordType = new RecordType();
            receiptRecordType = [SELECT ID,DeveloperName,Name,SObjectType, IsActive FROM RecordType where DeveloperName =: 'Main_Receipt' AND SObjectType =: 'Receipt__c' AND IsActive =: true LIMIT 1];
            
            if(invoiceObjectList.size() > 0)
            {
                //For Updating the Suspension Object first we need to Query the suspensions Id...
                List<ID> suspensionIdList = new List<ID>();
                           
                //For Creating the Receipt associated to that object...
                for(Invoice__c invoiceObject : invoiceObjectList)
                {
                    Receipt__c receiptObject = new Receipt__c();
                
                    receiptObject.Company__c = accountId;
                    receiptObject.Total_Amount_on_Invoice__c = invoiceObject.Total_Invoice_Formula_Field__c;
                    receiptObject.Receipt_Type__c = invoiceObject.Invoice_Type__c;
                    receiptObject.Related_Invoice__c = invoiceObject.Id;
                    receiptObject.Date__c = System.Today();
                    receiptObject.Request__c  = serviceLetterId;
                    receiptObject.RecordTypeId = receiptRecordType.Id;
                    
                    //Now Adding in the List ...
                    receiptList.add( receiptObject);
                    
                    for(Invoice_Line_Item__c invoiceItem : invoiceObject.Invoice_Line_Items__r)
                    {   
                        //Adding the Suspension Id...
                        suspensionIdList.add(invoiceItem.Suspension__c);    
                    }
                    
                    //Change the Invoice and Change the Status to Receipt Created...
                    invoiceObject.Receipt_Generated__c = true;
                    
                }
                
                //Now insert the receipt...
                if(receiptList.size() > 0)
                    insert receiptList;
                    
                //Now Update the Invoices...
                if(invoiceObjectList.size() > 0)
                    update invoiceObjectList;
                
                //Now Query for the Suspension Object to update the record....
                if(suspensionIdList.size() > 0)
                {
                    List<Fines__c> suspensionUpdateList = new List<Fines__c>();
                    
                    for(Fines__c suspensionObject : [Select ID,Cleared_Date__c,Status__c, Name from Fines__c where Id =: suspensionIdList])
                    {
                        suspensionObject.Cleared_Date__c = system.today();
                        suspensionObject.Status__c = 'Completed';
                        
                        //Adding in the List...
                        suspensionUpdateList.add(suspensionObject); 
                    }
                    
                    //Now Update the List...
                    if(suspensionUpdateList.size() > 0)
                        update suspensionUpdateList;
                    
                    
                }
                    
                //return receiptObject.Id;
                for(Receipt__c receiptObject : receiptList)
                {
                    receiptIdsList.add(receiptObject.Id);
                }
                //FOr Service Letter Update...
for(Request__c requestObject : [SELECT ID, Request_Status__c FROM Request__c where ID =:serviceLetterId])
{
    requestObject.Request_Status__c = 'Receipt Issued'; 
    update requestObject;
    break;
}
                return receiptIdsList;
            }           
            
            //Else return null... 
            return null;
        }
        catch(System.Exception exp)
        {
            Database.rollback(sp);
            //return exp.getMessage();
            System.debug('The Exception => '+ exp.getMessage());
            return null;
            
        }
    
    }
    
    //For Creating the Receipt Record Method On Application Object Button...
    WebService static List<string> generatingReceiptForApplicationMethod(ID accountId, List<ID> invoiceId,ID applicationId)
    {
        Savepoint sp = Database.setSavepoint();
        try
        {
            
            List<Invoice__c> invoiceObjectList = new List<Invoice__c>([Select Receipt_Generated__c,Total_Invoice_Formula_Field__c, Name, Invoice_Type__c, Id, Account__c,(Select Id,Name,Suspension__c from Invoice_Line_Items__r  where Suspension__c != null ) From Invoice__c where Id IN: invoiceId AND Account__c =: accountId]);
            List<Receipt__c> receiptList = new List<Receipt__c>();
            List<string> receiptIdsList = new List<string>();
            RecordType receiptRecordType = new RecordType();
            receiptRecordType = [SELECT ID,DeveloperName,Name,SObjectType, IsActive FROM RecordType where DeveloperName =: 'Main_Receipt' AND SObjectType =: 'Receipt__c' AND IsActive =: true LIMIT 1];
            
            if(invoiceObjectList.size() > 0)
            {
                //For Updating the Suspension Object first we need to Query the suspensions Id...
                List<ID> suspensionIdList = new List<ID>();
                           
                //For Creating the Receipt associated to that object...
                for(Invoice__c invoiceObject : invoiceObjectList)
                {
                    Receipt__c receiptObject = new Receipt__c();
                
                    receiptObject.Company__c = accountId;
                    receiptObject.Total_Amount_on_Invoice__c = invoiceObject.Total_Invoice_Formula_Field__c;
                    receiptObject.Receipt_Type__c = invoiceObject.Invoice_Type__c;
                    receiptObject.Related_Invoice__c = invoiceObject.Id;
                    receiptObject.Date__c = System.Today();
                    receiptObject.Application__c = applicationId;
                    receiptObject.RecordTypeId = receiptRecordType.Id;
                    
                    //Now Adding in the List ...
                    receiptList.add( receiptObject);
                    
                    for(Invoice_Line_Item__c invoiceItem : invoiceObject.Invoice_Line_Items__r)
                    {   
                        //Adding the Suspension Id...
                        suspensionIdList.add(invoiceItem.Suspension__c);    
                    }
                    
                    //Change the Invoice and Change the Status to Receipt Created...
                    invoiceObject.Receipt_Generated__c = true;
                    
                }
                
                //Now insert the receipt...
                if(receiptList.size() > 0)
                    insert receiptList;
                    
                //Now Update the Invoices...
                if(invoiceObjectList.size() > 0)
                    update invoiceObjectList;
                
                //Now Query for the Suspension Object to update the record....
                if(suspensionIdList.size() > 0)
                {
                    List<Fines__c> suspensionUpdateList = new List<Fines__c>();
                    
                    for(Fines__c suspensionObject : [Select ID,Cleared_Date__c,Status__c, Name from Fines__c where Id =: suspensionIdList])
                    {
                        suspensionObject.Cleared_Date__c = system.today();
                        suspensionObject.Status__c = 'Completed';
                        
                        //Adding in the List...
                        suspensionUpdateList.add(suspensionObject); 
                    }
                    
                    //Now Update the List...
                    if(suspensionUpdateList.size() > 0)
                        update suspensionUpdateList;
                    
                    
                }
                    
                //return receiptObject.Id;
                for(Receipt__c receiptObject : receiptList)
                {
                    receiptIdsList.add(receiptObject.Id);
                }
                
                return receiptIdsList;
            }           
            
            //Else return null... 
            return null;
        }
        catch(System.Exception exp)
        {
            Database.rollback(sp);
            //return exp.getMessage();
            System.debug('The Exception => '+ exp.getMessage());
            return null;
            
        }
    
    }
    
    //For Creating the Receipt Record Method On Application Object Button...
    WebService static List<string> generatingReceiptForTrackingMethod(ID accountId, List<ID> invoiceId,ID trackingId)
    {
        Savepoint sp = Database.setSavepoint();
        try
        {
            
            List<Invoice__c> invoiceObjectList = new List<Invoice__c>([Select Receipt_Generated__c,Total_Invoice_Formula_Field__c, Name, Invoice_Type__c, Id, Account__c,(Select Id,Name,Suspension__c from Invoice_Line_Items__r  where Suspension__c != null ) From Invoice__c where Id IN: invoiceId AND Account__c =: accountId]);
            
            List<Receipt__c> receiptList = new List<Receipt__c>();
            List<string> receiptIdsList = new List<string>();
            RecordType receiptRecordType = new RecordType();
            receiptRecordType = [SELECT ID,DeveloperName,Name,SObjectType, IsActive FROM RecordType where DeveloperName =: 'Main_Receipt' AND SObjectType =: 'Receipt__c' AND IsActive =: true LIMIT 1];
            
            if(invoiceObjectList.size() > 0)
            {
                //For Updating the Suspension Object first we need to Query the suspensions Id...
                List<ID> suspensionIdList = new List<ID>();
                              
                //For Creating the Receipt associated to that object...
                for(Invoice__c invoiceObject : invoiceObjectList)
                {
                    Receipt__c receiptObject = new Receipt__c();
                
                    receiptObject.Company__c = accountId;
                    receiptObject.Total_Amount_on_Invoice__c = invoiceObject.Total_Invoice_Formula_Field__c;
                    receiptObject.Receipt_Type__c = invoiceObject.Invoice_Type__c;
                    receiptObject.Related_Invoice__c = invoiceObject.Id;
                    receiptObject.Date__c = System.Today();
                    receiptObject.Tracking__c = trackingId;
                    receiptObject.RecordTypeId = receiptRecordType.Id;
                    //Now Adding in the List ...
                    receiptList.add( receiptObject);
                    
                    for(Invoice_Line_Item__c invoiceItem : invoiceObject.Invoice_Line_Items__r)
                    {   
                        //Adding the Suspension Id...
                        suspensionIdList.add(invoiceItem.Suspension__c);    
                    }
                    
                    //Change the Invoice and Change the Status to Receipt Created...
                    invoiceObject.Receipt_Generated__c = true;
                    
                }
                
                //Now insert the receipt...
                if(receiptList.size() > 0)
                    insert receiptList;
                    
                //Now Update the Invoices...
                if(invoiceObjectList.size() > 0)
                    update invoiceObjectList;
                
                //Now Query for the Suspension Object to update the record....
                if(suspensionIdList.size() > 0)
                {
                    List<Fines__c> suspensionUpdateList = new List<Fines__c>();
                    
                    for(Fines__c suspensionObject : [Select ID,Cleared_Date__c,Status__c, Name from Fines__c where Id =: suspensionIdList])
                    {
                        suspensionObject.Cleared_Date__c = system.today();
                        suspensionObject.Status__c = 'Completed';
                        
                        //Adding in the List...
                        suspensionUpdateList.add(suspensionObject); 
                    }
                    
                    //Now Update the List...
                    if(suspensionUpdateList.size() > 0)
                        update suspensionUpdateList;
                    
                    
                }
                    
                //return receiptObject.Id;
                for(Receipt__c receiptObject : receiptList)
                {
                    receiptIdsList.add(receiptObject.Id);
                }
                
                return receiptIdsList;
            }           
            
            //Else return null... 
            return null;
        }
        catch(System.Exception exp)
        {
            Database.rollback(sp);
            //return exp.getMessage();
            System.debug('The Exception => '+ exp.getMessage());
            return null;
            
        }
    
    }
    
    
    
    
    //For Creating the Receipt Record Company License Number Assignment Method On Account Object Button...
    //WebService static string generatingReceiptForAmendmentMethod(ID accountId, ID invoiceId,ID amendmentId)
    WebService static List<string> generatingReceiptForAmendmentMethod(ID accountId, List<ID> invoiceId,ID amendmentId)
    {
        Savepoint sp = Database.setSavepoint();
        try
        {
            
            //List<Invoice__c> invoiceObjectList = new List<Invoice__c>([Select Receipt_Generated__c,Total_Invoice_Formula_Field__c, Name, Invoice_Type__c, Id, Account__c,(Select Id,Name,Suspension__c from Invoice_Line_Items__r  where Suspension__c != null ) From Invoice__c where Id=: invoiceId AND Account__c =: accountId]);
            List<Invoice__c> invoiceObjectList = new List<Invoice__c>([Select Receipt_Generated__c,Total_Invoice_Formula_Field__c, Name, Invoice_Type__c, Id, Account__c,(Select Id,Name,Suspension__c from Invoice_Line_Items__r  where Suspension__c != null ) From Invoice__c where Id IN: invoiceId AND Account__c =: accountId]);
            
            List<Receipt__c> receiptList = new List<Receipt__c>();
            List<string> receiptIdsList = new List<string>();
            RecordType receiptRecordType = new RecordType();
            receiptRecordType = [SELECT ID,DeveloperName,Name,SObjectType, IsActive FROM RecordType where DeveloperName =: 'Main_Receipt' AND SObjectType =: 'Receipt__c' AND IsActive =: true LIMIT 1];
            
            if(invoiceObjectList.size() > 0)
            {
                /*Receipt__c receiptObject = new Receipt__c();
                
                receiptObject.Company__c = accountId;
                receiptObject.Total_Amount_on_Invoice__c = invoiceObjectList[0].Total_Invoice_Formula_Field__c;
                receiptObject.Receipt_Type__c = invoiceObjectList[0].Invoice_Type__c;
                receiptObject.Related_Invoice__c = invoiceObjectList[0].Id;
                receiptObject.Date__c = System.Today();
                receiptObject.Amendment__c = amendmentId;
                receiptObject.RecordTypeId = receiptRecordType.Id;
                //Now Inserting the receiptObject...
                insert receiptObject;*/
                
            
                //For Updating the Suspension Object first we need to Query the suspensions Id...
                List<ID> suspensionIdList = new List<ID>();
                
                for(Invoice__c invoiceObject : invoiceObjectList)
                {
                    Receipt__c receiptObject = new Receipt__c();
                
                    receiptObject.Company__c = accountId;
                    receiptObject.Total_Amount_on_Invoice__c = invoiceObject.Total_Invoice_Formula_Field__c;
                    receiptObject.Receipt_Type__c = invoiceObject.Invoice_Type__c;
                    receiptObject.Related_Invoice__c = invoiceObject.Id;
                    receiptObject.Date__c = System.Today();
                    receiptObject.Amendment__c = amendmentId;
                    receiptObject.RecordTypeId = receiptRecordType.Id;
                    
                    //Now Adding in the List ...
                    receiptList.add( receiptObject);
                    
                    for(Invoice_Line_Item__c invoiceItem : invoiceObject.Invoice_Line_Items__r)
                    {   
                        //Adding the Suspension Id...
                        suspensionIdList.add(invoiceItem.Suspension__c);    
                    }
                    
                    //Change the Invoice and Change the Status to Receipt Created...
                    invoiceObject.Receipt_Generated__c = true;
                    
                }
                
                //Now insert the receipt...
                if(receiptList.size() > 0)
                    insert receiptList;
                    
                //Now Update the Invoices...
                if(invoiceObjectList.size() > 0)
                    update invoiceObjectList;
                    
                //Now Query for the Suspension Object to update the record....
                if(suspensionIdList.size() > 0)
                {
                    List<Fines__c> suspensionUpdateList = new List<Fines__c>();
                    
                    for(Fines__c suspensionObject : [Select ID,Cleared_Date__c,Status__c, Name from Fines__c where Id =: suspensionIdList])
                    {
                        suspensionObject.Cleared_Date__c = system.today();
                        suspensionObject.Status__c = 'Completed';
                        
                        //Adding in the List...
                        suspensionUpdateList.add(suspensionObject); 
                    }
                    
                    //Now Update the List...
                    if(suspensionUpdateList.size() > 0)
                        update suspensionUpdateList;
                    
                    
                }
                
                
                Amendment__c amendmentObject = new Amendment__c();
                amendmentObject = [select Id,Amendment_Application_Status__c from Amendment__c where Id =:amendmentId];
                
                if(amendmentObject != null)
                {
                    amendmentObject.Amendment_Application_Status__c = 'Receipt Issued';
                    update amendmentObject;
                }
                
                
                //Returning the receipt Object Ids..
                //return receiptObject.Id;
                for(Receipt__c receiptObject : receiptList)
                {
                    receiptIdsList.add(receiptObject.Id);
                }
                
                return receiptIdsList;
            }           
            
            //Else return null... 
            return null;
        }
        catch(System.Exception exp)
        {
            Database.rollback(sp);
            //return exp.getMessage();
            System.debug('The Exception => '+ exp.getMessage());
            return null;
            
        }
    
    }
    
    //For Creating the Receipt Record Company License Number Assignment Method On Tenancy Contract Object Button...
    //WebService static string generatingReceiptForTenancyContractMethod(ID accountId, ID invoiceId,ID tenancyId)
    WebService static List<string> generatingReceiptForTenancyContractMethod(ID accountId, List<ID> invoiceId,ID tenancyId)
    {
        Savepoint sp = Database.setSavepoint();
        try
        {
            Tenancy_Contract__c tenancyObject = new Tenancy_Contract__c();
            tenancyObject = [select Id,Tenancy_Contract_Renewal_Steps__c from Tenancy_Contract__c where Id =: tenancyId];
            //List<Invoice__c> invoiceObjectList = new List<Invoice__c>([Select Receipt_Generated__c,Total_Invoice_Formula_Field__c, Name, Invoice_Type__c, Id, Account__c,(Select Id,Name,Suspension__c from Invoice_Line_Items__r  where Suspension__c != null ) From Invoice__c where Id=: invoiceId AND Account__c =: accountId]);
            List<Invoice__c> invoiceObjectList = new List<Invoice__c>([Select Receipt_Generated__c,Total_Invoice_Formula_Field__c, Name, Invoice_Type__c, Id, Account__c,(Select Id,Name,Suspension__c from Invoice_Line_Items__r  where Suspension__c != null ) From Invoice__c where Id IN: invoiceId AND Account__c =: accountId]);
            
            List<Receipt__c> receiptList = new List<Receipt__c>();
            List<string> receiptIdsList = new List<string>();
            
            RecordType receiptRecordType = new RecordType();
            receiptRecordType = [SELECT ID,DeveloperName,Name,SObjectType, IsActive FROM RecordType where DeveloperName =: 'Main_Receipt' AND SObjectType =: 'Receipt__c' AND IsActive =: true LIMIT 1];
            
            if(invoiceObjectList.size() > 0)
            {
                /*Receipt__c receiptObject = new Receipt__c();
                
                receiptObject.Company__c = accountId;
                receiptObject.Total_Amount_on_Invoice__c = invoiceObjectList[0].Total_Invoice_Formula_Field__c;
                receiptObject.Receipt_Type__c = invoiceObjectList[0].Invoice_Type__c;
                receiptObject.Related_Invoice__c = invoiceObjectList[0].Id;
                receiptObject.Date__c = System.Today();
                receiptObject.Tenancy_Contract__c = tenancyId;
                receiptObject.RecordTypeId = receiptRecordType.Id;
                //Now Inserting the receiptObject...
                insert receiptObject;*/
                
                
                
                //For Updating the Suspension Object first we need to Query the suspensions Id...
                List<ID> suspensionIdList = new List<ID>();
                
                for(Invoice__c invoiceObject : invoiceObjectList)
                {
                    Receipt__c receiptObject = new Receipt__c();
                    
                    receiptObject.Company__c = accountId;
                    receiptObject.Total_Amount_on_Invoice__c = invoiceObject.Total_Invoice_Formula_Field__c;
                    receiptObject.Receipt_Type__c = invoiceObject.Invoice_Type__c;
                    receiptObject.Related_Invoice__c = invoiceObject.Id;
                    receiptObject.Date__c = System.Today();
                    receiptObject.Tenancy_Contract__c = tenancyId;
                    receiptObject.RecordTypeId = receiptRecordType.Id;
                    //Now Adding in the receipt List...
                    receiptList.add(receiptObject);
                    
                    for(Invoice_Line_Item__c invoiceItem : invoiceObject.Invoice_Line_Items__r)
                    {   
                        //Adding the Suspension Id...
                        suspensionIdList.add(invoiceItem.Suspension__c);    
                    }
                    
                    //Change the Invoice and Change the Status to Receipt Created...
                    invoiceObject.Receipt_Generated__c = true;
                }
                
                //Now Insert the Receipts...
                if(receiptList.size() > 0)
                    insert receiptList;
                    
                //Update the Tenancy Contract Renewal Step...
                tenancyObject.Tenancy_Contract_Renewal_Steps__c = 'Receipt Issued';
                update tenancyObject;
                
                //Update the Invoices...
                if(invoiceObjectList.size() > 0)
                    update invoiceObjectList;
                
                //Now Query for the Suspension Object to update the record....
                if(suspensionIdList.size() > 0)
                {
                    List<Fines__c> suspensionUpdateList = new List<Fines__c>();
                    
                    for(Fines__c suspensionObject : [Select ID,Cleared_Date__c,Status__c, Name from Fines__c where Id =: suspensionIdList])
                    {
                        suspensionObject.Cleared_Date__c = system.today();
                        suspensionObject.Status__c = 'Completed';
                        
                        //Adding in the List...
                        suspensionUpdateList.add(suspensionObject); 
                    }
                    
                    //Now Update the List...
                    if(suspensionUpdateList.size() > 0)
                        update suspensionUpdateList;
                    
                    
                }
                
                //Returning the receipt Object Ids..
                //return receiptObject.Id;
                for(Receipt__c receiptObject : receiptList)
                {
                    receiptIdsList.add(receiptObject.Id);
                    
                }
                
                return receiptIdsList;
            }           
            
            //Else return null... 
            return null;
        }
        catch(System.Exception exp)
        {
            Database.rollback(sp);
            //return exp.getMessage();
            System.debug('The Exception is => '+ exp.getMessage());
            return null;
        
        }
    
    }
    
    
    //For Creating the Receipt Record Company License Number Assignment Method...(On Invoice Object).....
    WebService static string generatingReceiptMethod(ID accountId, ID invoiceId, string invoiceType,string companyLicenseNumber,string invoiceTotal)
    {
        Savepoint sp = Database.setSavepoint();
        try
        {
            RecordType receiptRecordType = new RecordType();
            receiptRecordType = [SELECT ID,DeveloperName,Name,SObjectType, IsActive FROM RecordType where DeveloperName =: 'Main_Receipt' AND SObjectType =: 'Receipt__c' AND IsActive =: true LIMIT 1];
            
            System.debug('The Invoice Total => '+invoiceTotal.replace(',', ''));
            Receipt__c receiptObject = new Receipt__c();
            decimal invoiceAmount = decimal.valueof(invoiceTotal.replace(',', ''));
            
            System.debug('The Invoice Amount => '+invoiceAmount);
            
            receiptObject.Company__c = accountId;
            //receiptObject.Total_Amount_on_Invoice__c = invoiceAmount;
            
            if(invoiceTotal != '')
                receiptObject.Total_Amount_on_Invoice__c = invoiceAmount;
            receiptObject.Receipt_Type__c = invoiceType;
            receiptObject.Related_Invoice__c = invoiceId;
            receiptObject.Date__c = System.Today();
            receiptObject.RecordTypeId = receiptRecordType.Id;
            
            //Now Inserting the receiptObject...
            insert receiptObject;
            
            System.debug('The Receipt Id => '+ receiptObject.Id);
            
            List<ID> suspensionIdList = new List<ID>();
            
            for(Invoice_Line_Item__c invoiceItem : [Select Id,Name,Suspension__c from Invoice_Line_Item__c  where Suspension__c != null and  Invoice__c =: invoiceId])
            {
                //Adding the Suspension Id...
                suspensionIdList.add(invoiceItem.Suspension__c);    
                
            }
            
            if(suspensionIdList.size() > 0)
            {
                List<Fines__c> suspensionUpdateList = new List<Fines__c>();
                    
                for(Fines__c suspensionObject : [Select ID,Cleared_Date__c,Status__c, Name from Fines__c where Id =: suspensionIdList])
                {
                    suspensionObject.Cleared_Date__c = system.today();
                    suspensionObject.Status__c = 'Completed';
                    
                    //Adding in the List...
                    suspensionUpdateList.add(suspensionObject); 
                }
                
                //Now Update the List...
                if(suspensionUpdateList.size() > 0)
                    update suspensionUpdateList;
            }
            
            //Get the Invoice and Change the Status to Receipt Created...
            Invoice__c invoiceObject = new Invoice__c();
            invoiceObject = [select Id,Receipt_Generated__c from Invoice__c where Id =: invoiceId ];
            
            if(invoiceObject != null)
            {
                invoiceObject.Receipt_Generated__c = true;
                update invoiceObject;
                
            }
            
            //This code is correct this is only comment because We dont need to update the account because this method is used in the Invoice Object....            
            /*List<Account> accountObject = new List<Account>([select Id,LicenseNumber__c,License_Application_Step__c from Account where Id =:accountId]);
            
            if(companyLicenseNumber == '' || companyLicenseNumber == null)
            {
                System.debug('The Company License Id => '+ companyLicenseNumber);
                List<License_Serial__c> licenseSerialObject = new List<License_Serial__c>([select Id,Name, Status__c from License_Serial__c where Status__c = 'Available' LIMIT 1]);
                    
                if(licenseSerialObject.size() > 0)
                {
                    System.debug('The Invoice Total => '+invoiceTotal);
                    
                    if(accountObject.size() > 0)
                    {
                        System.debug('In Account Total => ');
                        accountObject[0].LicenseNumber__c = licenseSerialObject[0].Id;
                        accountObject[0].License_Application_Step__c = 'Receipt Issued';
                        //licenseSerialObject[0].Status__c = 'Reserved';
                        licenseSerialObject[0].Status__c = 'Leased';
                        
                        
                        System.debug('In Account Total => before update');
                        //Now Update the License Serial and Account ...
                        update accountObject;
                        update licenseSerialObject;
                        
                    }
                    
                }
                else
                {
                   if(accountObject.size() > 0)
                   {
                        System.debug('In Else Statement');
                       accountObject[0].License_Application_Step__c = 'Receipt Issued';
                       update accountObject;
                   }
                }
                
            }
            else
            {
               if(accountObject.size() > 0)
               {
                    System.debug('In Else Statement');
                   accountObject[0].License_Application_Step__c = 'Receipt Issued';
                   update accountObject;
               }
            }*/
            
            
            //Returning the receipt Object Ids..
            return receiptObject.Id;
                        
            
        }
        catch(System.Exception exp)
        {
            Database.rollback(sp);
            //return exp.getMessage();
            System.debug('The Exception => '+ exp.getMessage());
            return null;
        
        }
    
    }
    //For Updating the Company Information Methods for Renewal of License....
    Webservice static string confirmCompanyRegistrationForRenewalLicenseMethod(ID accountId,string renewalType, string licenseType)
    {
        Savepoint sp = Database.setSavepoint();
        try
        {
            System.debug('The Renewal Type : '+ renewalType);
            System.debug('The Account ID : '+accountId);
            //if(!ProcessorControlClass.isSimultaneouslyRun)
            //{
            
            //if(accountId == '' || renewalType == '' )
            //    return 'Some Unexpected Error occur';
            
            
                Account accountObject = null;
            
                accountObject = [select Renewal_Process_Start_Date__c,id,RecordTypeId, License_Issue_Date__c,License_Application_Step__c, License_Expiry_Date__c, License_State_Date__c from Account where Id =: accountId];
                
                if(renewalType == 'License Renewal')
                {
                            
                    if(accountObject != null)
                    {
                        RecordType recordTypeObject = [Select IsActive, Id, DeveloperName,SObjectType, Description From RecordType  where IsActive = true AND DeveloperName =: 'Renewal_Front_Office_Confirmed' AND SObjectType =: 'Account'];
                        System.debug('The Previous License Expiry Date : '+ accountObject.License_Expiry_Date__c);
                        accountObject.Renewal_Process_Start_Date__c = System.Today();
                        //Previous Requirement 22/May/2012
                        accountObject.License_Issue_Date__c = accountObject.License_Expiry_Date__c.adddays(1);
                        accountObject.License_Expiry_Date__c = (accountObject.License_Expiry_Date__c).addyears(1);
                        
                        System.debug('The New License Expiry Date : '+ accountObject.License_Expiry_Date__c);
                        
                        /*accountObject.License_Issue_Date__c = accountObject.License_Expiry_Date__c;
                        accountObject.License_Expiry_Date__c = (accountObject.License_Expiry_Date__c).addyears(1).adddays(-1);*/
                        accountObject.RecordTypeId = recordTypeObject.Id;
                        accountObject.isTenancyCreated__c = false;
                        
                        update accountObject;
                        
                        //return 'Success';
                    }
                    
                    
                        
                }
                else if(renewalType == 'Renewal Additional')
                {
                    if(accountObject != null)
                    {
                        
                        //For First Checking the Confirm Account...
                        for(Account_Location__c accountLocationObject : [select id, Delete_Flag__c,New_Location__r.name,New_Location__r.Current_Tenancy_Contract__c ,New_Location__c FROM Account_Location__c where Account__c =: accountObject.Id AND isRenewed__c =: false AND IsDeleted =: false AND New_Location__c != : null])
                        {
                            System.debug('The Location Current Tenancy Contract => '+ accountLocationObject.New_Location__r.Current_Tenancy_Contract__c);
                            if(accountLocationObject.New_Location__r.Current_Tenancy_Contract__c != null)
                            {
                                return 'The Location '+ accountLocationObject.New_Location__r.name + ' is associated to the other company, please remove this location';
                            }
                        }
                        
                        
                        RecordType recordTypeObject = [Select IsActive, Id, DeveloperName,SObjectType, Description From RecordType  where IsActive = true AND DeveloperName =: 'Renewal_Additional_Front_Office_Confirmed' AND SObjectType =: 'Account'];
                        
                        accountObject.Renewal_Process_Start_Date__c = System.Today();
                        //Previous Requirement 22/May/2012
                        accountObject.License_Issue_Date__c = accountObject.License_Expiry_Date__c.adddays(1);
                        accountObject.License_Expiry_Date__c = (accountObject.License_Expiry_Date__c).addyears(1);
                        /*accountObject.License_Issue_Date__c = accountObject.License_Expiry_Date__c;
                        accountObject.License_Expiry_Date__c = (accountObject.License_Expiry_Date__c).addyears(1).adddays(-1);*/
                        accountObject.RecordTypeId = recordTypeObject.Id;
                        accountObject.isTenancyCreated__c = false;
                        
                        update accountObject;
                        
                        //return 'Success';
                    }
                    
                }
                
                
                List<Receipt__c> receiptUpdateList = new List<Receipt__c>();
                    
                //Now Update the Account Associated Receipt to make sure after this step user can't cancell this Receipt...(11/Feb/2013)
                for(Receipt__c receiptObject : [SELECT id,Company__c, Fee_Application_Type__c,isProcessConfirmed__c, CreatedDate,is_Cancelled_Receipt__c  FROM  Receipt__c where Company__c =: accountId AND Fee_Application_Type__c =: licenseType AND is_Cancelled_Receipt__c = false AND isProcessConfirmed__c =: false])
                {
                    receiptObject.isProcessConfirmed__c = true;
                    //Adding in the List...
                    receiptUpdateList.add(receiptObject);   
                }
                
                //Now Update the Amendment 
                if(receiptUpdateList.size() > 0)
                    update receiptUpdateList;
                    
                    
            //}
            
            //Assigning the Process Control Flag...
            //ProcessorControlClass.isSimultaneouslyRun = true;
            
            //Returning the Values...
            return 'Success';
            
            //Return the values...
            return null;            
        
        }
        catch(System.Exception exp)
        {
            System.debug('The Excetion : '+ exp.getLineNumber() + ' : '+ exp.getMessage());
            Database.rollback(sp);
            return exp.getMessage();
        }
    }
    
    //For Updating the Company & Creating Contacts,Passport History and Shared Ownership Records Method For New License...
    Webservice static string confirmCompanyRegistrationForNewLicenseMethod(ID accountId, string LicenseType)
    {
        Savepoint sp = Database.setSavepoint();
        try
        {
            if(!ProcessorControlClass.isSimultaneouslyRun)
            {
                //RecordType partyInformationRecordTypeForNew = [select Id,SObjectType,IsActive,DeveloperName from RecordType where SObjectType =: 'Party_Information__c' AND IsActive =: true AND DeveloperName =:'New'];
                Map<Id,String> partyInformationRecordTypeMap = new Map<Id,string>();
                for(RecordType partyInformationRecordType : [select Id,SObjectType,IsActive,DeveloperName from RecordType where SObjectType =: 'Party_Information__c' AND IsActive =: true])
                {
                    partyInformationRecordTypeMap.put(partyInformationRecordType.Id,partyInformationRecordType.DeveloperName);
                    //partyInformationRecordTypeMap.put(partyInformationRecordType.DeveloperName,partyInformationRecordType.Id);
                }
                
                //Account accountObject = [select Id, RecordTypeId, License_Issue_Date__c,License_Application_Step__c,  License_Expiry_Date__c, License_State_Date__c,(Select Id,Existing_Contact__c,RecordTypeId,Role__c, Name, Address_Arabic__c, Address__c, Email_Address__c, Land_Number__c, Mobile_Number__c, Passport_Expiry_Date__c, Date_Of_Birth__c,Passport_Issue_Date__c, Passport_Number__c, Preferred_Language__c, Title__c, First_Name__c, Last_Name__c, Arabic_Full_Name_c__c, Passport_Issue_Country__c,Ownership__c,Gender__c,Share_Profit__c,Country__c From Shareholders_Information__r where RecordTypeId =: partyInformationRecordTypeForNew.Id) from Account where Id =: accountId];
                Account accountObject = [select Id, RecordTypeId,File_Number__c, License_Issue_Date__c,License_Application_Step__c,  License_Expiry_Date__c, License_State_Date__c,(Select Id,Existing_Contact__c,RecordTypeId,Role__c, Name, Address_Arabic__c, Address__c, Email_Address__c, Land_Number__c, Mobile_Number__c, Passport_Expiry_Date__c, Date_Of_Birth__c,Passport_Issue_Date__c, Passport_Number__c, Preferred_Language__c, Title__c, First_Name__c, Last_Name__c, Arabic_Full_Name_c__c, Passport_Issue_Country__c,Ownership__c,Gender__c,Share_Profit__c,Country__c From Shareholders_Information__r) from Account where Id =: accountId];
                //Account accountObject = [select Id, RecordTypeId, License_Issue_Date__c,License_Application_Step__c,  License_Expiry_Date__c, License_State_Date__c,(Select Id,Existing_Contact__c,RecordTypeId,Role__c, Name, Address_Arabic__c, Address__c, Email_Address__c, Land_Number__c, Mobile_Number__c, Passport_Expiry_Date__c, Date_Of_Birth__c,Passport_Issue_Date__c, Passport_Number__c, Preferred_Language__c, Title__c, First_Name__c, Last_Name__c, Arabic_Full_Name_c__c, Passport_Issue_Country__c,Ownership__c,Gender__c,Share_Profit__c,Country__c From Shareholders_Information__r where RecordTypeId =: partyInformationRecordTypeMap.get('New')) from Account where Id =: accountId];
                Account masterAccount = [select Id,Master_Account_Flag__c, Name from Account where Master_Account_Flag__c =: true LIMIT 1];
                List<Contact> contactObjectList = new List<Contact>();
                List<Passport_History__c> passportHistoryObjectList = new List<Passport_History__c>();
                List<Share_Ownership__c> sharedOwnershipObjectTempList = new List<Share_Ownership__c>();
                List<Share_Ownership__c> sharedOwnershipObjectList = new List<Share_Ownership__c>();
                List<Share_Ownership__c> sharedOwnershipObjectListForExistingParty = new List<Share_Ownership__c>();
                if(accountObject != null)
                {
                    
                    //For First Checking the Confirm Account...
                    for(Location_Booking_Information__c locationBookingObject : [select id, Location__r.name,Location__r.Current_Tenancy_Contract__c ,Location__c FROM Location_Booking_Information__c where Account__c =: accountObject.Id])
                    {
                        if(locationBookingObject.Location__r.Current_Tenancy_Contract__c != null)
                        {
                            return 'The Location '+ locationBookingObject.Location__r.name + ' is associated to the other company, please remove this location';
                        }
                    }
                    
                    
                    //RecordType recordTypeObject = [Select IsActive, Id, DeveloperName, Description From RecordType  where IsActive = true AND DeveloperName =: 'Registered'];
                    RecordType recordTypeObject = [Select IsActive, Id, DeveloperName, Description From RecordType  where IsActive = true AND DeveloperName =: 'Under_Process_Front_Office_Confirmed'];
                    
                    if(recordTypeObject != null)
                    {
                        //Account Setting ....
                        accountObject.License_Issue_Date__c = system.Today();
                        accountObject.License_State_Date__c = system.Today();
                        accountObject.License_Expiry_Date__c = System.Today().addyears(1).adddays(-1);
                        accountObject.RecordTypeId = recordTypeObject.Id;
                        
                        //For Creating the Contacts based on the 
                        for(Party_Information__c partyAccountObject : accountObject.Shareholders_Information__r)
                        {
                            if(partyInformationRecordTypeMap.get(partyAccountObject.RecordTypeId) == 'New')
                            {
                                //For New Contact....
                                Contact tempContact = new Contact();
                                
                                //tempContact.AccountId = accountObject.Id;
                                
                                //Associate COntacts to Master AFZA Account...
                                tempContact.AccountId = masterAccount.Id;
                                tempContact.Salutation = partyAccountObject.Title__c;
                                tempContact.Title = partyAccountObject.Title__c;
                                tempContact.FirstName = partyAccountObject.First_Name__c;
                                tempContact.LastName = partyAccountObject.Last_Name__c;
                                tempContact.Arabic_Name__c = partyAccountObject.Arabic_Full_Name_c__c;
                                tempContact.Birthdate = partyAccountObject.Date_Of_Birth__c;
                                tempContact.MobilePhone = partyAccountObject.Mobile_Number__c;
                                tempContact.Phone  = partyAccountObject.Land_Number__c;
                                tempContact.Nationality__c = partyAccountObject.Country__c;
                                tempContact.Email = partyAccountObject.Email_Address__c;
                                tempContact.Preferred_Language__c = partyAccountObject.Preferred_Language__c;
                                tempContact.Address_English__c = partyAccountObject.Address__c;
                                tempContact.Address_Arabic__c = partyAccountObject.Address_Arabic__c;
                                tempContact.Gender__c = partyAccountObject.Gender__c;
                                //tempContact.Current_Employer__c = accountObject.Id;
                                
                                //Set the Authorize Manager in the Account Object...
                                if(String.ValueOf(partyAccountObject.Role__c).Contains('Manager'))
                                    tempContact.isManagerReference__c = true;
                                
                                /*if(String.ValueOf(partyAccountObject.Role__c).Contains('Manager') && String.ValueOf(partyAccountObject.Role__c).Contains('Shareholder'))
                                {
                                    //tempContact.Contact_Type__c = 'Shareholder and Manager';
                                }
                                else if(String.ValueOf(partyAccountObject.Role__c).Contains('Manager'))
                                {
                                    //tempContact.Contact_Type__c = 'Manager';
                                     
                                }
                                else
                                {
                                    tempContact.Contact_Type__c = 'Shareholder';
                                }*/
                                
                                
                                //For Passport History of this User...
                                Passport_History__c tempPassportHistory = new Passport_History__c();
                                
                                tempPassportHistory.Passport_Number__c = partyAccountObject.Passport_Number__c;
                                tempPassportHistory.Passport_Issue_Date__c = partyAccountObject.Passport_Issue_Date__c;
                                tempPassportHistory.Passport_Expiry_Date__c = partyAccountObject.Passport_Expiry_Date__c;
                                tempPassportHistory.Passport_Issue_Country__c = partyAccountObject.Passport_Issue_Country__c;
                                //Adding in the Passport History List...
                                passportHistoryObjectList.add(tempPassportHistory);
                                System.debug('The New Party Account Role => '+ partyAccountObject.Role__c);
                                
                                
                                //Set the Contact Flag for Share Holder...
                                if(String.ValueOf(partyAccountObject.Role__c).Contains('Shareholder'))
                                    tempContact.isShareholderReference__c = true;
                            
                                //For Shared OwnerShip...
                                Share_Ownership__c tempShareOwnership = new Share_Ownership__c();
                                tempShareOwnership.Company__c = accountObject.Id;
                                tempShareOwnership.Ownership__c = partyAccountObject.Ownership__c;
                                tempShareOwnership.Share_Profit__c = partyAccountObject.Share_Profit__c;
                                                            
                                //Adding in the Shared Ownership List..
                                sharedOwnershipObjectTempList.add(tempShareOwnership);
                                
                               
                                
                                System.debug('The Share Holder Reference => '+ tempContact.isShareholderReference__c);
                                
                                
                                
                                //Adding in the Contact List...
                                contactObjectList.add(tempContact);
                                
                                
                                System.debug('The New Shared Ownership in New SIze => '+ sharedOwnershipObjectTempList.size());
                                
                            }
                            else if(partyInformationRecordTypeMap.get(partyAccountObject.RecordTypeId) == 'Existing')
                            {
                                //Set the Authorize Manager in the Account Object...
                                if(String.ValueOf(partyAccountObject.Role__c).Contains('Manager'))
                                    accountObject.Authorized_Manager__c  = partyAccountObject.Existing_Contact__c;
                                
                                //Only Share Holer Role has a Share Ownership record...
                                if(String.ValueOf(partyAccountObject.Role__c).Contains('Shareholder'))
                                {   
                                    //For Shared OwnerShip...
                                    Share_Ownership__c tempShareOwnership = new Share_Ownership__c();
                                    tempShareOwnership.Company__c = accountObject.Id;
                                    tempShareOwnership.Shareholder__c = partyAccountObject.Existing_Contact__c;
                                    tempShareOwnership.Ownership__c = partyAccountObject.Ownership__c;
                                    tempShareOwnership.Share_Profit__c = partyAccountObject.Share_Profit__c;
                                    
                                    //Adding in the Shared Ownership List..
                                    sharedOwnershipObjectListForExistingParty.add(tempShareOwnership);
                                    
                                    System.debug('The Existing Shared Ownership in New SIze => '+ sharedOwnershipObjectListForExistingParty.size()); 
                                }   
                            }
                            
                            
                        }
                        
                        
                    }
                    
                    
                    //For New COntacts...
                    //Now Insert the COntact Object to get the ids and mapped in the passport History and sharedOwnership Object...
                    if(contactObjectList.size() > 0)
                    {
                        //Now Insert the records...
                        insert contactObjectList;
                        
                        
                        //for(Contact contactObject : contactObjectList)
                        for(integer counter = 0; counter < contactObjectList.size() ; counter++)
                        {
                            //For Passport Holder...
                            passportHistoryObjectList[counter].Passport_Holder__c = contactObjectList[counter].Id;
                            passportHistoryObjectList[counter].isCurrent__c = true;
                            
                            //If the Contact is only Manager Not Share holder.... we need to remove the value from the list...
                            /*if(contactObjectList[counter].isManagerReference__c == true && contactObjectList[counter].isShareholderReference__c == false)
                            {
                                System.debug('The System in Only Manager => ');
                                sharedOwnershipObjectList.remove(counter);
                            }*/ 
                            //For Shared ownership...New
                            if(contactObjectList[counter].isShareholderReference__c == true)
                            {
                                System.debug('The Share Holder Reference => '+ contactObjectList[counter].isShareholderReference__c);                               
                                System.debug('The Share Ownership Name => '+ sharedOwnershipObjectTempList[counter].Shareholder_English_Name__c + ' && The Share Ownership Shareholder => '+ sharedOwnershipObjectTempList[counter].Shareholder__c);
                                //if(sharedOwnershipObjectList[counter].Shareholder__c == null || sharedOwnershipObjectList[counter].Shareholder__c == '')
                                sharedOwnershipObjectTempList[counter].Shareholder__c = contactObjectList[counter].Id;
                                
                                
                                //Put the Share holder Ownership record in other list...
                                sharedOwnershipObjectList.add(sharedOwnershipObjectTempList[counter]);
                           
                           
                            }
                            
                            System.debug('Only Check the Manager Reference');
                            //Set the Authorize Manager in the Account Object...
                            if(contactObjectList[counter].isManagerReference__c == true)
                                accountObject.Authorized_Manager__c = contactObjectList[counter].Id;
                            /*if(contactObjectList[counter].Contact_Type__c == 'Shareholder and Manager')
                                accountObject.Authorized_Manager__c = contactObjectList[counter].Id;
                            else if (contactObjectList[counter].Contact_Type__c == 'Manager')   
                                accountObject.Authorized_Manager__c = contactObjectList[counter].Id; */
                        }
                        
                        //Now Inserting the Passport History and Shared Ownership records...
                        if(passportHistoryObjectList.size() > 0)
                            insert  passportHistoryObjectList;
                        
                        //Its a old approach when we did not implement the isCurrent Code...
                        /*//for Updating the Contacts with the passport History Data...
                        for(integer counter = 0; counter < passportHistoryObjectList.size() ; counter++)
                        {
                            //Put the Passport History Id...
                            contactObjectList[counter].Passport_Number__c = passportHistoryObjectList[counter].Id;
                            
                        
                        }
                        
                        //Now Update the Contact...
                        if(contactObjectList.size() > 0)
                            update contactObjectList;*/
                        
                        
                                
                    }
                    
                    System.debug('The Total Number of Shared Ownership => '+ sharedOwnershipObjectList.size());
                    //For Share ownership... For New Contacts...
                    if(sharedOwnershipObjectList.size() > 0)
                        insert sharedOwnershipObjectList;
                    if(sharedOwnershipObjectListForExistingParty.size() > 0)
                        insert sharedOwnershipObjectListForExistingParty;
                        
                    //For File Number Calculation.... The combination of (Total License StateDate + 1 Slash this year
                    List<Account> accountFileNumberList = new List<Account>([select id,License_State_Date__c from Account where License_State_Date__c = THIS_YEAR]);
                    
                    //Now Mapped the ids with the passport history and shared ownership objects...
                    accountObject.File_Number__c = (accountFileNumberList.size() + 1) + '/' + System.today().year();   
                    accountObject.License_Application_Step__c = 'Contacts Created';
                    //Now Update the Account ...
                    update accountObject;
                    
                    List<Receipt__c> receiptUpdateList = new List<Receipt__c>();
                    
                    //Now Update the Account Associated Receipt to make sure after this step user can't cancell this Receipt...(11/Feb/2013)
                    for(Receipt__c receiptObject : [SELECT id,Company__c, Fee_Application_Type__c,isProcessConfirmed__c, CreatedDate,is_Cancelled_Receipt__c  FROM  Receipt__c where Company__c =: accountId AND Fee_Application_Type__c =: licenseType AND is_Cancelled_Receipt__c = false AND isProcessConfirmed__c =: false])
                    {
                        receiptObject.isProcessConfirmed__c = true;
                        //Adding in the List...
                        receiptUpdateList.add(receiptObject);   
                    }
                    
                    //True the Process Control flag ...
                    ProcessorControlClass.isSimultaneouslyRun = true;
                    
                    
                    //Now Update the Amendment 
                    if(receiptUpdateList.size() > 0)
                        update receiptUpdateList;
                    
               
                }
                
                return 'Success';
                
                
            }
            
            return null;
            
        }
        catch(System.Exception exp)
        {
            Database.rollback(sp);
            return exp.getMessage();
        }
    
    }
       
    
    //For Updating the Company & Creating Contacts,Passport History and Shared Ownership Records Method For New License...
    Webservice static List<string> creatingTenancyContractForNewLicenseMethod(ID accountId)
    {
        Savepoint sp = Database.setSavepoint();
        try
        {
            Account accountObject = [select Id, RecordTypeId,isTenancyCreated__c, License_Issue_Date__c,License_Application_Step__c,  License_Expiry_Date__c, License_State_Date__c,(Select Id, Account__c, License_Booking_Status__c, Location__c, Booking_Date__c, Booking_Leasing_Issue_Date__c, Booking_Released_Date__c,Location__r.Rental_Rate__c, Location__r.Name, Location__r.Area__c,Location__r.Area__r.Name, Location__r.Fee__c, Location__r.Area__r.Project_Type__c, Location__r.Current_Tenancy_Contract__c,Location__r.Current_Tenancy_Contract__r.Name, Location__r.Current_Tenancy_Contract__r.Contract_Status__c,Location__r.Current_Tenancy_Contract__r.Location_type__c, Location__r.Current_Tenancy_Contract__r.Rental_Rate__c,Location__r.Current_Tenancy_Contract__r.Tenancy_Contract_Expiry_Date__c, Location__r.Visa_Quota__c, Location__r.Current_Tenancy_Contract__r.Tenancy_Contract_Start_Date__c, Location__r.Current_Tenancy_Contract__r.Total_Visas_Available__c  From Locations_Booking_Information__r) from Account where Id =: accountId];
            
            List<Tenancy_Contract__c> tenancyContractList = new List<Tenancy_Contract__c>();
            list<String> resultList = new list<string>();
            List<Location_Booking_Information__c> locationBookingList = new List<Location_Booking_Information__c>();
            RecordType recordType = [select Id,IsActive,SObjectType,DeveloperName from RecordType where DeveloperName = 'New_Tenancy_Contract' AND SObjectType = 'Tenancy_Contract__c'];
            //List<Invoice__c> invoiceList = new List<Invoice__c>([SELECT Id,]);
            
            Map<ID,Invoice_Line_Item__c> invoiceItemMap = new Map<Id,Invoice_Line_Item__c>();
            
            if(accountObject != null)
            {
                //Invoice Always One...
                //for(Invoice__c invoiceObject : [Select Invoice_Type__c, Id, Fee_Application_Type__c, Cancelled__c, Account__c,CreatedDate, (Select Id, Name, Invoice__c, Amount__c, Fee__c, Location_Booking_Information__c,Location_Booking_Information__r.id, Location__c, Package_Value__c, Action__c, Installment__c, Payment_Plan__c From Invoice_Line_Items__r WHERE Name =: 'Location Rental Rate') From Invoice__c WHERE Account__c =: accountId AND Invoice_Type__c =: 'Non Refundable' AND Fee_Application_Type__c =: 'New License' AND Cancelled__c =: false ORDER BY CreatedDate DESC LIMIT 1])
                for(Invoice__c invoiceObject : [Select Invoice_Type__c, Id, Fee_Application_Type__c, Cancelled__c, Account__c,CreatedDate, (Select Id, Start_Date__c,End_Date__c,Name, Invoice__c, Amount__c, Fee__c, Location_Booking_Information__c,Location_Booking_Information__r.id, Location__c, Package_Value__c, Action__c, Installment__c, Payment_Plan__c From Invoice_Line_Items__r WHERE Name LIKE: 'Location Rental Rate%') From Invoice__c WHERE Account__c =: accountId AND Invoice_Type__c =: 'Non Refundable' AND Fee_Application_Type__c =: 'New License' AND Cancelled__c =: false ORDER BY CreatedDate DESC LIMIT 1])
                {
                    for(Invoice_Line_Item__c invoiceItem : invoiceObject.Invoice_Line_Items__r )
                    {
                        //Putting in the Map...
                        invoiceItemMap.put(invoiceItem.Location_Booking_Information__r.id, invoiceItem);
                    }   
                }
                
                
                System.debug('The Number of Invoice Item Map =: '+ invoiceItemMap.size());  
                //Now Creating the Tenancy COntract... (Package Implementation)
                for(Location_Booking_Information__c bookingObject : accountObject.Locations_Booking_Information__r)
                {
                    Tenancy_Contract__c tempTenancy = new Tenancy_Contract__c();
                    tempTenancy.RecordTypeId = recordType.id;
                    tempTenancy.Location__c = bookingObject.Location__c;
                    System.debug('The Invoice Map => '+ invoiceItemMap.get(bookingObject.Id));
                    
                    //If not Find...
                    tempTenancy.Tenancy_Contract_Start_Date__c = accountObject.License_Issue_Date__c;
                    tempTenancy.Tenancy_Contract_Expiry_Date__c = date.TODAY().addyears(1).adddays(-1);
                    
                    if(invoiceItemMap.get(bookingObject.Id) != null)
                    {
                        tempTenancy.Rental_Rate__c =invoiceItemMap.get(bookingObject.Id).Amount__c;
                        tempTenancy.Package_Value__c = invoiceItemMap.get(bookingObject.Id).Package_Value__c;
                        tempTenancy.Payment_Plan__c = invoiceItemMap.get(bookingObject.Id).Payment_Plan__c;
                        tempTenancy.Action__c = invoiceItemMap.get(bookingObject.Id).Action__c;
                        tempTenancy.Installment__c = invoiceItemMap.get(bookingObject.Id).Installment__c;
                        tempTenancy.Tenancy_Contract_Start_Date__c = invoiceItemMap.get(bookingObject.Id).Start_Date__c;
                        tempTenancy.Tenancy_Contract_Expiry_Date__c = invoiceItemMap.get(bookingObject.Id).End_Date__c;
                    }   
                    else
                        tempTenancy.Rental_Rate__c = bookingObject.Location__r.Rental_Rate__c;
                    
                    
                    tempTenancy.Fee_Application_Type__c = 'New License';
                    
                    tempTenancy.Tenant__c = accountObject.id;
                    tempTenancy.Total_Visas_Available__c = bookingObject.Location__r.Visa_Quota__c;
                    
                    //Adding in the list...
                    tenancyContractList.add(tempTenancy);
                    
                    //Change the Location Leasing date and Status...
                    bookingObject.License_Booking_Status__c = 'Occupied';
                    bookingObject.Booking_Leasing_Issue_Date__c = system.Today();          
                    
                    //Adding in the list to update....
                    locationBookingList.add(bookingObject);
                    
                    
                    
                    
                    
                }
                //Before 16th October 2012... (Correct but before Payment Plan)
                /*for(Location_Booking_Information__c bookingObject : accountObject.Locations_Booking_Information__r)
                {
                    Tenancy_Contract__c tempTenancy = new Tenancy_Contract__c();
                    tempTenancy.RecordTypeId = recordType.id;
                    tempTenancy.Location__c = bookingObject.Location__c;
                    tempTenancy.Fee__c = bookingObject.Location__r.Fee__c;
                    tempTenancy.Rental_Rate__c = bookingObject.Location__r.Rental_Rate__c;
                    tempTenancy.Tenancy_Contract_Expiry_Date__c = date.TODAY().addyears(1).adddays(-1);
                    //tempTenancy.Tenancy_Contract_Start_Date__c = date.TODAY();
                    tempTenancy.Fee_Application_Type__c = 'New License';
                    tempTenancy.Tenancy_Contract_Start_Date__c = accountObject.License_Issue_Date__c;
                    tempTenancy.Tenant__c = accountObject.id;
                    tempTenancy.Total_Visas_Available__c = bookingObject.Location__r.Visa_Quota__c;
                    
                    //Adding in the list...
                    tenancyContractList.add(tempTenancy);
                    
                    //Change the Location Leasing date and Status...
                    bookingObject.License_Booking_Status__c = 'Occupied';
                    bookingObject.Booking_Leasing_Issue_Date__c = system.Today();          
                    
                    //Adding in the list to update....
                    locationBookingList.add(bookingObject);    
                }
                */
                if(tenancyContractList.size() > 0)
                {
                    //First insert the tenancy contract...
                    insert tenancyContractList;
                    
                    //Updating the task...
                    update locationBookingList;
                    
                    //Now Update the Location Booking Selected Location Field (Current Tenancy Contract);
                    List<ID> LocationIDs = new List<ID>();
                    for(Tenancy_Contract__c tempTenancy : tenancyContractList)
                    {
                        //Putting in the Result List...
                        resultList.add(tempTenancy.Id);
                        LocationIDs.Add(tempTenancy.Location__c);
                    }
                    
                    //For Changing the current Tenancy Contract with the new one...
                    List<Location__c> LocationsList = [SELECT id, Current_Tenancy_Contract__c FROM Location__c WHERE id IN :LocationIDs];
                    
                    For(Tenancy_Contract__c tempTenancy : tenancyContractList)
                    {
                        For(Location__c Location : LocationsList)
                        {
                            if(tempTenancy.Location__c == Location.id)
                            {
                                Location.Current_Tenancy_Contract__c = tempTenancy.id;
                                break;
                            }
                        }
                    }
                    
                    //Updating the Location
                    update(LocationsList);
                    
                    //Now Updating the Tenancy Contract Flag on the COmpany...
                    accountObject.isTenancyCreated__c = true;
                    accountObject.License_Application_Step__c = 'Tenancy Contract Issued';
                    update accountObject;
                    
                }
                
            }            
            
            //Returning the Ids...
            return resultList;
        }
        catch(System.Exception exp)
        {
            Database.rollback(sp);
            return null;
        }
    }
    
    
    //For Creating the Tenancy Contract Method For Renewal Additional (Add Location & Change Location ) License...
    Webservice static List<string> creatingTenancyContractForRenewalAdditionalLicenseMethod(ID accountId)
    {
        Savepoint sp = Database.setSavepoint();
        try
        {
            Account accountObject = null;
            accountObject = [select Id, RecordTypeId,isTenancyCreated__c, License_Issue_Date__c,License_Application_Step__c,  License_Expiry_Date__c, License_State_Date__c,(Select Id, Account__c, License_Booking_Status__c, Location__c, Booking_Date__c, Booking_Leasing_Issue_Date__c, Booking_Released_Date__c,Location__r.Rental_Rate__c, Location__r.Name, Location__r.Area__c,Location__r.Area__r.Name, Location__r.Fee__c, Location__r.Area__r.Project_Type__c, Location__r.Current_Tenancy_Contract__c,Location__r.Current_Tenancy_Contract__r.Name, Location__r.Current_Tenancy_Contract__r.Contract_Status__c,Location__r.Current_Tenancy_Contract__r.Location_type__c, Location__r.Current_Tenancy_Contract__r.Rental_Rate__c,Location__r.Current_Tenancy_Contract__r.Tenancy_Contract_Expiry_Date__c, Location__r.Visa_Quota__c, Location__r.Current_Tenancy_Contract__r.Tenancy_Contract_Start_Date__c, Location__r.Current_Tenancy_Contract__r.Total_Visas_Available__c  From Locations_Booking_Information__r) from Account where Id =: accountId];
            
            //For Account Locations...
            List<Account_Location__c> accountLocationList = new List<Account_Location__c>([Select New_Location__r.Id,New_Location__r.Rental_Rate__c,Account_Expiry_Date__c,renewAddLocationFlag__c,Previous_Tenancy_Contract__r.Location__r.Location_Type__c,Previous_Tenancy_Contract__r.Account_Today_Expiry_Date__c,Previous_Tenancy_Contract__r.Location__r.Rental_Rate__c,Previous_Tenancy_Contract__r.Account_Expiry_Date__c,isRenewed__c, Previous_Tenancy_Contract__r.Fine_Per_Day__c, Previous_Tenancy_Contract__r.Tenancy_Contract_Cancellation_Date__c, Previous_Tenancy_Contract__r.Location_type__c, Previous_Tenancy_Contract__r.Rental_Rate__c, Previous_Tenancy_Contract__r.Tenancy_Contract_Expiry_Date__c, Previous_Tenancy_Contract__r.Tenancy_Contract_Start_Date__c, Previous_Tenancy_Contract__r.Tenant__c, Previous_Tenancy_Contract__r.Location__c, Previous_Tenancy_Contract__r.Id, Previous_Tenancy_Contract__c, Previous_Location__c, New_Location__c, Name, Id, Flag__c, Edit_Flag__c, Delete_Flag__c, Add_Flag__c, Account__c From Account_Location__c where Account__c =: accountId AND Delete_Flag__c =: false AND isRenewed__c =: false]);
            
            List<Tenancy_Contract__c> tenancyContractList = new List<Tenancy_Contract__c>();
            List<Tenancy_Contract__c> tenancyContractPreviousRecordList = new List<Tenancy_Contract__c>();
            List<Location_Booking_Information__c> locationBookingList = new List<Location_Booking_Information__c>();
            list<String> resultList = new list<string>();
            List<Id> accountLocationPreviousTenancyIds = new List<Id>();
            List<Id> accountLocationPreviousTenancyCancellationIds = new List<Id>();
            
            RecordType tenancyContractRecordType = new RecordType();
            tenancyContractRecordType = [select id,SObjectType,DeveloperName,IsActive,Name from RecordType where SObjectType =: 'Tenancy_Contract__c' AND IsActive =: true AND DeveloperName =:'New_Tenancy_Contract'];  
            
            Map<ID,Invoice_Line_Item__c> invoiceItemMap = new Map<Id,Invoice_Line_Item__c>();
            
            
            if(accountObject != null)
            {
                //Invoice Always One...
                //for(Invoice__c invoiceObject : [Select Invoice_Type__c, Id, Fee_Application_Type__c, Cancelled__c, Account__c,CreatedDate, (Select Id, Name, Invoice__c, Amount__c, Fee__c, Location_Booking_Information__c,Location_Booking_Information__r.id, Location__c,Location__r.Id, Package_Value__c, Action__c, Installment__c,Tenancy_Contract__c,Tenancy_Contract__r.Id, Payment_Plan__c From Invoice_Line_Items__r WHERE Name =: 'Location Rental Rate') From Invoice__c WHERE Account__c =: accountId AND Invoice_Type__c =: 'Non Refundable' AND Fee_Application_Type__c =: 'Renewal Amendment' AND Cancelled__c =: false ORDER BY CreatedDate DESC LIMIT 1])
                for(Invoice__c invoiceObject : [Select Invoice_Type__c, Id, Fee_Application_Type__c, Cancelled__c, Account__c,CreatedDate, (Select Id, Name, Invoice__c,Start_Date__c, End_Date__c, Amount__c, Fee__c, Location_Booking_Information__c,Location_Booking_Information__r.id, Location__c,Location__r.Id, Package_Value__c, Action__c, Installment__c,Tenancy_Contract__c,Tenancy_Contract__r.Id, Payment_Plan__c From Invoice_Line_Items__r WHERE Name LIKE: 'Location Rental Rate%') From Invoice__c WHERE Account__c =: accountId AND Invoice_Type__c =: 'Non Refundable' AND Fee_Application_Type__c =: 'Renewal Amendment' AND Cancelled__c =: false ORDER BY CreatedDate DESC LIMIT 1])
                {
                    for(Invoice_Line_Item__c invoiceItem : invoiceObject.Invoice_Line_Items__r )
                    {
                        //Putting in the Map...
                        invoiceItemMap.put(invoiceItem.Location__r.Id, invoiceItem);
                    }   
                }
                
                //For Location Booking Invoice and Invoice Line Item..
                if(accountLocationList.size() > 0)
                {
                    for(Account_Location__c bookingObject : accountLocationList)
                    {
                        //For Add Location (Change Location )....
                        if(bookingObject.renewAddLocationFlag__c != true && bookingObject.New_Location__c != null)
                        {
                            //Now Create a Tenancy Contract for this...
                            Tenancy_Contract__c tenancyObject = new Tenancy_Contract__c();
                            tenancyObject.Tenant__c = accountId;
                            tenancyObject.RecordTypeId = tenancyContractRecordType.id;
                            //tenancyObject.Fee_Application_Type__c = 'Renewal Additional';
                            tenancyObject.Fee_Application_Type__c = 'Renewal Amendment';
                            tenancyObject.Location__c = bookingObject.New_Location__c;
                            tenancyObject.Tenancy_Contract_Start_Date__c = System.today().adddays(1);
                            tenancyObject.Tenancy_Contract_Expiry_Date__c = bookingObject.Account_Expiry_Date__c;
                            
                            //For Location Rental Rate... (Package Implementation)
                            if(invoiceItemMap.get(bookingObject.New_Location__r.Id) != null)
                            {
                                tenancyObject.Rental_Rate__c =invoiceItemMap.get(bookingObject.New_Location__r.Id).Amount__c;
                                tenancyObject.Package_Value__c = invoiceItemMap.get(bookingObject.New_Location__r.Id).Package_Value__c;
                                tenancyObject.Payment_Plan__c = invoiceItemMap.get(bookingObject.New_Location__r.Id).Payment_Plan__c;
                                tenancyObject.Action__c = invoiceItemMap.get(bookingObject.New_Location__r.Id).Action__c;
                                tenancyObject.Installment__c = invoiceItemMap.get(bookingObject.New_Location__r.Id).Installment__c;
                                
                                //Adding on 25th Nov 2013...
                                tenancyObject.Tenancy_Contract_Start_Date__c = invoiceItemMap.get(bookingObject.New_Location__r.Id).Start_Date__c;
                                tenancyObject.Tenancy_Contract_Expiry_Date__c = invoiceItemMap.get(bookingObject.New_Location__r.Id).End_Date__c;
                                
                            }
                            else
                            {
                                if(System.today().addyears(1) == bookingObject.Account_Expiry_Date__c)
                                    tenancyObject.Rental_Rate__c = bookingObject.New_Location__r.Rental_Rate__c;
                                else if(System.today().addyears(1) > bookingObject.Account_Expiry_Date__c) //Comes after the License Expiry Date...
                                {
                                    if(System.today().addyears(1) > bookingObject.Account_Expiry_Date__c)
                                    {
                                        Integer yearDifference = System.Today().addyears(1).year() - bookingObject.Account_Expiry_Date__c.year(); 
                                        
                                        tenancyObject.Tenancy_Contract_Start_Date__c = System.Today().adddays(1);
                                        tenancyObject.Tenancy_Contract_Expiry_Date__c = bookingObject.Account_Expiry_Date__c.addyears(yearDifference);
                                        Decimal daybetweenValue = Decimal.valueof((System.today()).daysbetween(bookingObject.Account_Expiry_Date__c.addyears(yearDifference))).setScale(2);
                                        tenancyObject.Rental_Rate__c = ((bookingObject.New_Location__r.Rental_Rate__c / 360) * (daybetweenValue)).setScale(2);
                                    }
                                    else
                                    {
                                        Decimal daybetweenValue = Decimal.valueof((System.today()).daysbetween(bookingObject.Account_Expiry_Date__c)).setScale(2);
                                        tenancyObject.Rental_Rate__c = ((bookingObject.New_Location__r.Rental_Rate__c / 360) * (daybetweenValue)).setScale(2);  
                                    }
                                    
                                }
                                else if(System.today().addyears(1) < bookingObject.Account_Expiry_Date__c) //Comes Before Expiry Date...
                                {
                                    Decimal daybetweenValue = Decimal.valueof((System.today()).daysbetween(bookingObject.Account_Expiry_Date__c)).setScale(2);
                                    tenancyObject.Rental_Rate__c = ((bookingObject.New_Location__r.Rental_Rate__c.setScale(2)) + ( (bookingObject.New_Location__r.Rental_Rate__c / 360) * daybetweenValue)).setScale(2) ;
                                }   
                            }    
                            
                            //Adding in the New Tenancy Contract List ...
                            tenancyContractList.add(tenancyObject);
                            
                            
                            //For Previous Tenancy Contract...(Only need to Cancel the Previous tenancy Contract)
                            if(bookingObject.Previous_Tenancy_Contract__c != null)
                                accountLocationPreviousTenancyCancellationIds.add(bookingObject.Previous_Tenancy_Contract__c);
                            
                            
                        }
                        //This Code doesn't Need to used...
                        else
                        {
                            //For Previous Tenancy Contract...
                            if(bookingObject.Previous_Tenancy_Contract__c != null)
                            {
                                accountLocationPreviousTenancyIds.add(bookingObject.Previous_Tenancy_Contract__c);
                            }
                            
                        }
                        
                        //Now Set the Account Location isRenewed Flag to true...
                        bookingObject.isRenewed__c = true;
                        
                        
                    }
                    //Before the 16th October 2012 (Before Package Implementation)
                    /*for(Account_Location__c bookingObject : accountLocationList)
                    {
                        //For Add Location (Change Location )....
                        if(bookingObject.renewAddLocationFlag__c != true && bookingObject.New_Location__c != null)
                        {
                            //Now Create a Tenancy Contract for this...
                            Tenancy_Contract__c tenancyObject = new Tenancy_Contract__c();
                            tenancyObject.Tenant__c = accountId;
                            tenancyObject.RecordTypeId = tenancyContractRecordType.id;
                            //tenancyObject.Fee_Application_Type__c = 'Renewal Additional';
                            tenancyObject.Fee_Application_Type__c = 'Renewal Amendment';
                            tenancyObject.Location__c = bookingObject.New_Location__c;
                            
                            if(System.today().addyears(1) == bookingObject.Account_Expiry_Date__c)
                            {
                                tenancyObject.Tenancy_Contract_Start_Date__c = System.today().adddays(1);
                                tenancyObject.Tenancy_Contract_Expiry_Date__c = bookingObject.Account_Expiry_Date__c;
                                tenancyObject.Rental_Rate__c = bookingObject.New_Location__r.Rental_Rate__c;
                            }
                            else if(System.today().addyears(1) > bookingObject.Account_Expiry_Date__c) //Comes after the License Expiry Date...
                            {
                                tenancyObject.Tenancy_Contract_Start_Date__c = System.today().adddays(1);
                                tenancyObject.Tenancy_Contract_Expiry_Date__c = bookingObject.Account_Expiry_Date__c;
                                //Decimal daybetweenValue = Decimal.valueof((System.today().addyears(1)).daysbetween(bookingObject.Account_Expiry_Date__c)).setScale(2);
                                Decimal daybetweenValue = Decimal.valueof((System.today()).daysbetween(bookingObject.Account_Expiry_Date__c)).setScale(2);
                                tenancyObject.Rental_Rate__c = ((bookingObject.New_Location__r.Rental_Rate__c / 360) * (daybetweenValue)).setScale(2);
                                
                            }
                            else if(System.today().addyears(1) < bookingObject.Account_Expiry_Date__c) //Comes Before Expiry Date...
                            {
                                tenancyObject.Tenancy_Contract_Start_Date__c = System.today().adddays(1);
                                tenancyObject.Tenancy_Contract_Expiry_Date__c = bookingObject.Account_Expiry_Date__c;
                                //.addyears(-1) because we calculate the 
                                //Decimal daybetweenValue = Decimal.valueof((System.today()).daysbetween(bookingObject.Account_Expiry_Date__c.addyears(-1))).setScale(2);
                                Decimal daybetweenValue = Decimal.valueof((System.today()).daysbetween(bookingObject.Account_Expiry_Date__c)).setScale(2);
                                tenancyObject.Rental_Rate__c = ((bookingObject.New_Location__r.Rental_Rate__c.setScale(2)) + ( (bookingObject.New_Location__r.Rental_Rate__c / 360) * daybetweenValue)).setScale(2) ;
                            }
                            
                            //Adding in the New Tenancy Contract List ...
                            tenancyContractList.add(tenancyObject);
                            
                            
                            //For Previous Tenancy Contract...(Only need to Cancel the Previous tenancy Contract)
                            if(bookingObject.Previous_Tenancy_Contract__c != null)
                                accountLocationPreviousTenancyCancellationIds.add(bookingObject.Previous_Tenancy_Contract__c);
                                
                        }
                        //This Code doesn't Need to used...
                        else
                        {
                            //For Previous Tenancy Contract...
                            if(bookingObject.Previous_Tenancy_Contract__c != null)
                            {
                                accountLocationPreviousTenancyIds.add(bookingObject.Previous_Tenancy_Contract__c);
                            }
                            
                        }
                        
                        //Now Set the Account Location isRenewed Flag to true...
                        bookingObject.isRenewed__c = true;
                            
                    }*/
                    
                }
                
                //For Cancellation of previous tenancy Contract...
                for(Tenancy_Contract__c tenancyObject : [Select Tenant__c,Location__r.Rental_Rate__c,Account_Today_Expiry_Date__c,Account_Expiry_Date__c,Fee_Application_Type__c,Tenancy_Contract_Cancellation_Date__c , Total_Visas_Available__c,Tenancy_Contract_Start_Date__c, Tenancy_Contract_Expiry_Date__c, Rental_Rate__c, Name, Location_type__c, Location__c, Id From Tenancy_Contract__c where ID in: accountLocationPreviousTenancyCancellationIds])
                {
                    //for Updating the Previous Tenant Object....
                    tenancyObject.Tenancy_Contract_Cancellation_Date__c = date.Today();
                    //Adding in the Updating List of Previous Records....
                    tenancyContractPreviousRecordList.add(tenancyObject);
                        
                }
                
                //Package Implementation...
                for(Tenancy_Contract__c tenancyObject : [Select Location__r.Current_Tenancy_Contract__c,Tenant__c,Location__r.Rental_Rate__c,Account_Today_Expiry_Date__c,Account_Expiry_Date__c,Fee_Application_Type__c,Tenancy_Contract_Cancellation_Date__c , Total_Visas_Available__c,Tenancy_Contract_Start_Date__c, Tenancy_Contract_Expiry_Date__c, Rental_Rate__c, Name, Location_type__c, Location__c,Location__r.Id, Id From Tenancy_Contract__c where ID in: accountLocationPreviousTenancyIds])
                {
                    if(tenancyObject.Tenancy_Contract_Expiry_Date__c <= tenancyObject.Account_Expiry_Date__c)
                    {
                        if(tenancyObject.Location__r.Current_Tenancy_Contract__c != null)
                        {
                            //We Need to clone the existing Records and then insert....
                            Tenancy_Contract__c tempTenantObject = new Tenancy_Contract__c();
                            tempTenantObject = tenancyObject.clone(false,true);
                            
                            //Now Change the Temp Application Fee Type to Renewal...
                            tempTenantObject.Fee_Application_Type__c = 'Renewal Additional';
                            tempTenantObject.RecordTypeId = tenancyContractRecordType.id;
                            tempTenantObject.Tenancy_Contract_Start_Date__c = tenancyObject.Tenancy_Contract_Expiry_Date__c.adddays(1);
                            tempTenantObject.Tenancy_Contract_Expiry_Date__c = (tenancyObject.Tenancy_Contract_Expiry_Date__c).addyears(1);
                            tempTenantObject.Location__c = tenancyObject.Location__c;
                            
                            //For Location Rental Rate (Package Implementation)...
                            if(invoiceItemMap.get(tenancyObject.Location__r.Id) != null)
                            {
                                tempTenantObject.Rental_Rate__c =invoiceItemMap.get(tenancyObject.Location__r.Id).Amount__c;
                                tempTenantObject.Package_Value__c = invoiceItemMap.get(tenancyObject.Location__r.Id).Package_Value__c;
                                tempTenantObject.Payment_Plan__c = invoiceItemMap.get(tenancyObject.Location__r.Id).Payment_Plan__c;
                                tempTenantObject.Action__c = invoiceItemMap.get(tenancyObject.Location__r.Id).Action__c;
                                tempTenantObject.Installment__c = invoiceItemMap.get(tenancyObject.Location__r.Id).Installment__c;
                                //Adding on 25th Nov 2013...
                                tempTenantObject.Tenancy_Contract_Start_Date__c = invoiceItemMap.get(tenancyObject.Location__r.Id).Start_Date__c;
                                tempTenantObject.Tenancy_Contract_Expiry_Date__c = invoiceItemMap.get(tenancyObject.Location__r.Id).End_Date__c;
                            }
                            else
                            {
                                if(tenancyObject.Account_Expiry_Date__c == (tenancyObject.Tenancy_Contract_Expiry_Date__c).addyears(1))
                                    tempTenantObject.Rental_Rate__c = tenancyObject.Location__r.Rental_Rate__c;
                                else if((tenancyObject.Tenancy_Contract_Expiry_Date__c < System.Today()) && (Decimal.valueof(System.Today().daysbetween((tenancyObject.Account_Expiry_Date__c).addyears(-1).adddays(1) )).setScale(2) < 30))
                                {
                                    Decimal daybetweenValue = Decimal.valueof(tenancyObject.Tenancy_Contract_Expiry_Date__c.daysbetween((tenancyObject.Account_Expiry_Date__c))).setScale(2);
                                    tempTenantObject.Rental_Rate__c = ((tenancyObject.Location__r.Rental_Rate__c / 360) * (daybetweenValue ));
                                    System.debug('tenancyObject.Tenancy_Contract_Expiry_Date__c < System.Today()) && (Decimal.valueof(System.Today().daysbetween((tenancyObject.Account_Expiry_Date__c).addyears(-1).adddays(-1) )).setScale(2) < 30) LOGIC => ' + Decimal.valueof(System.Today().daysbetween((tenancyObject.Account_Expiry_Date__c).addyears(-1).adddays(1) )).setScale(2));
                                }   
                                else
                                {
                                    Decimal daybetweenValue = Decimal.valueof(tenancyObject.Tenancy_Contract_Expiry_Date__c.daysbetween((tenancyObject.Account_Expiry_Date__c))).setScale(2);
                                    tempTenantObject.Rental_Rate__c = ((tenancyObject.Location__r.Rental_Rate__c / 360) * (daybetweenValue));
                                }   
                                
                                    
                            }       
                            
                            //for Updating the Previous Tenant Object....
                            tenancyObject.Tenancy_Contract_Cancellation_Date__c = date.Today();
                                                
                            //Adding in the Insertion List...
                            tenancyContractList.add(tempTenantObject);
                            
                            //Adding in the Updating List of Previous Records....
                            tenancyContractPreviousRecordList.add(tenancyObject);
                            
                                
                        }
                        else 
                        {
                            //for Updating the Previous Tenant Object....
                            tenancyObject.Tenancy_Contract_Cancellation_Date__c = date.Today();
                            
                            //Adding in the Updating List of Previous Records....
                            tenancyContractPreviousRecordList.add(tenancyObject);
                        }   
                        
                    }
                    else if(tenancyObject.Tenancy_Contract_Expiry_Date__c > tenancyObject.Account_Expiry_Date__c)//To Accomodate their messy data...
                    {
                        
                        //Putting in the Result List...
                        resultList.add(tenancyObject.Id);
                        
                    }
                    
                }     
                //Before 16th October 2012 (Before Package Implementation)
                /*//For Previous Tenancy Contract With Clones and Other Things......
                for(Tenancy_Contract__c tenancyObject : [Select Location__r.Current_Tenancy_Contract__c,Tenant__c,Location__r.Rental_Rate__c,Account_Today_Expiry_Date__c,Account_Expiry_Date__c,Fee_Application_Type__c,Tenancy_Contract_Cancellation_Date__c , Total_Visas_Available__c,Tenancy_Contract_Start_Date__c, Tenancy_Contract_Expiry_Date__c, Rental_Rate__c, Name, Location_type__c, Location__c, Id From Tenancy_Contract__c where ID in: accountLocationPreviousTenancyIds])
                {
                    if(tenancyObject.Tenancy_Contract_Expiry_Date__c <= tenancyObject.Account_Expiry_Date__c)
                    {
                        if(tenancyObject.Location__r.Current_Tenancy_Contract__c != null)
                        {
                            //We Need to clone the existing Records and then insert....
                            Tenancy_Contract__c tempTenantObject = new Tenancy_Contract__c();
                            tempTenantObject = tenancyObject.clone(false,true);
                            
                            //Now Change the Temp Application Fee Type to Renewal...
                            tempTenantObject.Fee_Application_Type__c = 'Renewal Additional';
                            tempTenantObject.RecordTypeId = tenancyContractRecordType.id;
                            tempTenantObject.Location__c = tenancyObject.Location__c;
                            
                            //For Setting the Start/Expiry Date of Tenancy Contract Logic...
                            if(tenancyObject.Account_Expiry_Date__c == (tenancyObject.Tenancy_Contract_Expiry_Date__c).addyears(1))
                            {
                                tempTenantObject.Tenancy_Contract_Start_Date__c = tenancyObject.Tenancy_Contract_Expiry_Date__c.adddays(1);
                                tempTenantObject.Tenancy_Contract_Expiry_Date__c = (tenancyObject.Tenancy_Contract_Expiry_Date__c).addyears(1);
                                tempTenantObject.Rental_Rate__c = tenancyObject.Location__r.Rental_Rate__c;
                            }
                            else if((tenancyObject.Tenancy_Contract_Expiry_Date__c < System.Today()) && (Decimal.valueof(System.Today().daysbetween((tenancyObject.Account_Expiry_Date__c).addyears(-1).adddays(1) )).setScale(2) < 30))
                            {
                                tempTenantObject.Tenancy_Contract_Start_Date__c = tenancyObject.Tenancy_Contract_Expiry_Date__c.adddays(1);
                                tempTenantObject.Tenancy_Contract_Expiry_Date__c = (tenancyObject.Account_Expiry_Date__c);
                                Decimal daybetweenValue = Decimal.valueof(tenancyObject.Tenancy_Contract_Expiry_Date__c.daysbetween((tenancyObject.Account_Expiry_Date__c))).setScale(2);
                                tempTenantObject.Rental_Rate__c = ((tenancyObject.Location__r.Rental_Rate__c / 360) * (daybetweenValue ));
                                
                                System.debug('tenancyObject.Tenancy_Contract_Expiry_Date__c < System.Today()) && (Decimal.valueof(System.Today().daysbetween((tenancyObject.Account_Expiry_Date__c).addyears(-1).adddays(-1) )).setScale(2) < 30) LOGIC => ' + Decimal.valueof(System.Today().daysbetween((tenancyObject.Account_Expiry_Date__c).addyears(-1).adddays(1) )).setScale(2));
                                
                            
                            }
                            else
                            {
                                tempTenantObject.Tenancy_Contract_Start_Date__c = tenancyObject.Tenancy_Contract_Expiry_Date__c.adddays(1);
                                tempTenantObject.Tenancy_Contract_Expiry_Date__c = (tenancyObject.Account_Expiry_Date__c);
                                Decimal daybetweenValue = Decimal.valueof(tenancyObject.Tenancy_Contract_Expiry_Date__c.daysbetween((tenancyObject.Account_Expiry_Date__c))).setScale(2);
                                tempTenantObject.Rental_Rate__c = ((tenancyObject.Location__r.Rental_Rate__c / 360) * (daybetweenValue));
                                
                                                         
                            }
       
                            //for Updating the Previous Tenant Object....
                            tenancyObject.Tenancy_Contract_Cancellation_Date__c = date.Today();
                                                
                            //Adding in the Insertion List...
                            tenancyContractList.add(tempTenantObject);
                            
                            //Adding in the Updating List of Previous Records....
                            tenancyContractPreviousRecordList.add(tenancyObject);
                        }
                        else 
                        {
                            //for Updating the Previous Tenant Object....
                            tenancyObject.Tenancy_Contract_Cancellation_Date__c = date.Today();
                            
                            //Adding in the Updating List of Previous Records....
                            tenancyContractPreviousRecordList.add(tenancyObject);
                        }   
                        
                    }
                    else if(tenancyObject.Tenancy_Contract_Expiry_Date__c > tenancyObject.Account_Expiry_Date__c)//To Accomodate their messy data...
                    {
                        
                        //Putting in the Result List...
                        resultList.add(tenancyObject.Id);
                        
                    }
                    
                }*/
                
                //For Location BOoking....   
                for(Location_Booking_Information__c bookingObject : accountObject.Locations_Booking_Information__r)
                {
                    System.debug('The Location Booking Id =>'+ bookingObject.Id);
                    //Change the Location Leasing date and Status...
                    bookingObject.License_Booking_Status__c = 'Occupied';
                    bookingObject.Booking_Leasing_Issue_Date__c = system.Today();          
                    
                    //Adding in the list to update....
                    locationBookingList.add(bookingObject);    
                    
                }            
                if(tenancyContractList.size() > 0)
                {
                    //First Updating the Previous Version...
                    update tenancyContractPreviousRecordList;
                    
                    //First insert the tenancy contract...
                    insert tenancyContractList;
                    
                    //Updating the task...
                    update locationBookingList;
                    
                    
                    //Now Update the Account Location....
                    if(accountLocationList.size() > 0)
                        update accountLocationList;
                    
                    //Now Update the Location Booking Selected Location Field (Current Tenancy Contract);
                    List<ID> LocationIDs = new List<ID>();
                    for(Tenancy_Contract__c tempTenancy : tenancyContractList)
                    {
                        //Putting in the Result List...
                        resultList.add(tempTenancy.Id);
                        LocationIDs.Add(tempTenancy.Location__c);
                    }
                    
                    //For Changing the current Tenancy Contract with the new one...
                    List<Location__c> LocationsList = [SELECT id, Current_Tenancy_Contract__c FROM Location__c WHERE id IN :LocationIDs];
                    
                    For(Tenancy_Contract__c tempTenancy : tenancyContractList)
                    {
                        For(Location__c Location : LocationsList)
                        {
                            if(tempTenancy.Location__c == Location.id)
                            {
                                Location.Current_Tenancy_Contract__c = tempTenancy.id;
                                break;
                            }
                        }
                    }
                    
                    //Updating the Location
                    update(LocationsList);
                    
                    
                    
                }
                
                if(resultList.size() > 0)
                {
                    //Now Updating the Tenancy Contract Flag on the COmpany...
                    accountObject.isTenancyCreated__c = true;
                    accountObject.License_Application_Step__c = 'Tenancy Contract Issued';
                    update accountObject;   
                }
                
            }
            
            //Returning the Ids...
            return resultList;
            
        }
        catch(System.Exception exp)
        {
            system.debug('The Exception is => '+ exp.getMessage()); 
            Database.rollback(sp);
            return null;
        }
    }
    
    
    //For Creating the Tenancy Contract Method For Renewal License...
    Webservice static List<string> creatingTenancyContractForRenewalLicenseMethod(ID accountId)
    {
        Savepoint sp = Database.setSavepoint();
        try
        {
            Account accountObject = new Account();
            accountObject = [select Id, RecordTypeId,isTenancyCreated__c, License_Issue_Date__c,License_Application_Step__c,  License_Expiry_Date__c, License_State_Date__c,(Select Id, Account__c, License_Booking_Status__c, Location__c, Booking_Date__c, Booking_Leasing_Issue_Date__c, Booking_Released_Date__c,Location__r.Rental_Rate__c, Location__r.Name, Location__r.Area__c,Location__r.Area__r.Name, Location__r.Fee__c, Location__r.Area__r.Project_Type__c, Location__r.Current_Tenancy_Contract__c,Location__r.Current_Tenancy_Contract__r.Name, Location__r.Current_Tenancy_Contract__r.Contract_Status__c,Location__r.Current_Tenancy_Contract__r.Location_type__c, Location__r.Current_Tenancy_Contract__r.Rental_Rate__c,Location__r.Current_Tenancy_Contract__r.Tenancy_Contract_Expiry_Date__c, Location__r.Visa_Quota__c, Location__r.Current_Tenancy_Contract__r.Tenancy_Contract_Start_Date__c, Location__r.Current_Tenancy_Contract__r.Total_Visas_Available__c  From Locations_Booking_Information__r) from Account where Id =: accountId];
            List<Tenancy_Contract__c> tenancyContractList = new List<Tenancy_Contract__c>();
            List<Tenancy_Contract__c> tenancyContractPreviousRecordList = new List<Tenancy_Contract__c>();
            List<Location_Booking_Information__c> locationBookingList = new List<Location_Booking_Information__c>();
            list<String> resultList = new list<string>();
            Map<ID,Invoice_Line_Item__c> invoiceItemMap = new Map<Id,Invoice_Line_Item__c>();
            
            RecordType tenancyContractRecordType = new RecordType();
            tenancyContractRecordType = [select id,SObjectType,DeveloperName,IsActive,Name from RecordType where SObjectType =: 'Tenancy_Contract__c' AND IsActive =: true AND DeveloperName =:'New_Tenancy_Contract'];  
            
            
            if(accountObject != null)
            {
                //Invoice Always One...
                //for(Invoice__c invoiceObject : [Select Invoice_Type__c, Id, Fee_Application_Type__c, Cancelled__c, Account__c,CreatedDate, (Select Id, Name, Invoice__c, Amount__c, Fee__c, Location_Booking_Information__c,Location_Booking_Information__r.id, Location__c, Package_Value__c, Action__c, Installment__c,Tenancy_Contract__c,Tenancy_Contract__r.Id, Payment_Plan__c From Invoice_Line_Items__r WHERE Name =: 'Location Rental Rate') From Invoice__c WHERE Account__c =: accountId AND Invoice_Type__c =: 'Non Refundable' AND Fee_Application_Type__c =: 'Renewal' AND Cancelled__c =: false ORDER BY CreatedDate DESC LIMIT 1])
                for(Invoice__c invoiceObject : [Select Invoice_Type__c, Id, Fee_Application_Type__c, Cancelled__c, Account__c,CreatedDate, (Select Id,Start_Date__c,End_Date__c ,Name, Invoice__c, Amount__c, Fee__c, Location_Booking_Information__c,Location_Booking_Information__r.id, Location__c, Package_Value__c, Action__c, Installment__c,Tenancy_Contract__c,Tenancy_Contract__r.Id, Payment_Plan__c From Invoice_Line_Items__r WHERE Name LIKE: 'Location Rental Rate%') From Invoice__c WHERE Account__c =: accountId AND Invoice_Type__c =: 'Non Refundable' AND Fee_Application_Type__c =: 'Renewal' AND Cancelled__c =: false ORDER BY CreatedDate DESC LIMIT 1])
                {
                    for(Invoice_Line_Item__c invoiceItem : invoiceObject.Invoice_Line_Items__r )
                    {
                        //Putting in the Map...
                        //invoiceItemMap.put(invoiceItem.Tenancy_Contract__r.id, invoiceItem);
                        invoiceItemMap.put(invoiceItem.Location__c, invoiceItem);
                    }   
                }
                
                for(Tenancy_Contract__c tenancyObject : [Select Tenant__c,Location__r.Rental_Rate__c,Account_Today_Expiry_Date__c,Account_Expiry_Date__c,Fee_Application_Type__c,Tenancy_Contract_Cancellation_Date__c , Total_Visas_Available__c,Tenancy_Contract_Start_Date__c, Tenancy_Contract_Expiry_Date__c, Rental_Rate__c, Name, Location_type__c, Location__c, Id From Tenancy_Contract__c where ((Tenancy_Contract_Cancellation_Date__c =: null) AND (Tenant__c =: accountId))])
                {
                    
                    if(tenancyObject.Tenancy_Contract_Expiry_Date__c <= tenancyObject.Account_Expiry_Date__c)
                    {
                        //We Need to clone the existing Records and then insert....
                        Tenancy_Contract__c tempTenantObject = new Tenancy_Contract__c();
                        tempTenantObject = tenancyObject.clone(false,true);
                        
                        //Now Change the Temp Application Fee Type to Renewal...
                        tempTenantObject.Fee_Application_Type__c = 'Renewal';
                        tempTenantObject.RecordTypeId = tenancyContractRecordType.id;
                        
                        tempTenantObject.Tenancy_Contract_Start_Date__c = tenancyObject.Tenancy_Contract_Expiry_Date__c.adddays(1);
                        tempTenantObject.Tenancy_Contract_Expiry_Date__c = (tenancyObject.Tenancy_Contract_Expiry_Date__c).addyears(1);
                        
                        //For Package Selection... Rental Rate...
                        //if(invoiceItemMap.get(tenancyObject.Id) != null)
                        if(invoiceItemMap.get(tenancyObject.Location__c) != null)
                        {
                            tempTenantObject.Rental_Rate__c =invoiceItemMap.get(tenancyObject.Location__c).Amount__c;
                            tempTenantObject.Package_Value__c = invoiceItemMap.get(tenancyObject.Location__c).Package_Value__c;
                            tempTenantObject.Payment_Plan__c = invoiceItemMap.get(tenancyObject.Location__c).Payment_Plan__c;
                            tempTenantObject.Action__c = invoiceItemMap.get(tenancyObject.Location__c).Action__c;
                            tempTenantObject.Installment__c = invoiceItemMap.get(tenancyObject.Location__c).Installment__c;
                            tempTenantObject.Tenancy_Contract_Start_Date__c = invoiceItemMap.get(tenancyObject.Location__c).Start_Date__c;
                            tempTenantObject.Tenancy_Contract_Expiry_Date__c = invoiceItemMap.get(tenancyObject.Location__c).End_Date__c;
                            
                            /*tempTenantObject.Rental_Rate__c =invoiceItemMap.get(tenancyObject.Id).Amount__c;
                            tempTenantObject.Package_Value__c = invoiceItemMap.get(tenancyObject.Id).Package_Value__c;
                            tempTenantObject.Payment_Plan__c = invoiceItemMap.get(tenancyObject.Id).Payment_Plan__c;
                            tempTenantObject.Action__c = invoiceItemMap.get(tenancyObject.Id).Action__c;
                            tempTenantObject.Installment__c = invoiceItemMap.get(tenancyObject.Id).Installment__c;
                            tempTenantObject.Tenancy_Contract_Start_Date__c = invoiceItemMap.get(tenancyObject.Id).Start_Date__c;
                            tempTenantObject.Tenancy_Contract_Expiry_Date__c = invoiceItemMap.get(tenancyObject.Id).End_Date__c; */
                        }
                        else //I think this one is not necessary because Tenancy Contract always map with Invoice Item Tenancy Contract Field...
                        {
                            if(tenancyObject.Account_Expiry_Date__c == (tenancyObject.Tenancy_Contract_Expiry_Date__c).addyears(1))
                                tempTenantObject.Rental_Rate__c = tenancyObject.Location__r.Rental_Rate__c;
                            else if((tenancyObject.Tenancy_Contract_Expiry_Date__c < System.Today()) && (Decimal.valueof(System.Today().daysbetween((tenancyObject.Account_Expiry_Date__c).addyears(-1).adddays(1) )).setScale(2) < 30))
                            {
                                Decimal daybetweenValue = Decimal.valueof(tenancyObject.Tenancy_Contract_Expiry_Date__c.daysbetween((tenancyObject.Account_Expiry_Date__c))).setScale(2);
                                tempTenantObject.Rental_Rate__c = ((tenancyObject.Location__r.Rental_Rate__c / 360) * (daybetweenValue ));
                            }
                            else
                            {
                                Decimal daybetweenValue = Decimal.valueof(tenancyObject.Tenancy_Contract_Expiry_Date__c.daysbetween((tenancyObject.Account_Expiry_Date__c))).setScale(2);
                                tempTenantObject.Rental_Rate__c = ((tenancyObject.Location__r.Rental_Rate__c / 360) * (daybetweenValue));
                                
                            }   
                                
                        }
                        
                        //for Updating the Previous Tenant Object....
                        tenancyObject.Tenancy_Contract_Cancellation_Date__c = date.Today();
                                            
                        //Adding in the Insertion List...
                        tenancyContractList.add(tempTenantObject);
                        
                        //Adding in the Updating List of Previous Records....
                        tenancyContractPreviousRecordList.add(tenancyObject);   
                        
                    }
                    else if(tenancyObject.Tenancy_Contract_Expiry_Date__c > tenancyObject.Account_Expiry_Date__c)//To Accomodate their messy data...
                    {
                        
                        //Putting in the Result List...
                        resultList.add(tenancyObject.Id);
                        
                    }
                    
                     
                     
                }
                
                //Before 16th October 2012 ( Before Payment Package Selection)
                /*for(Tenancy_Contract__c tenancyObject : [Select Tenant__c,Location__r.Rental_Rate__c,Account_Today_Expiry_Date__c,Account_Expiry_Date__c,Fee_Application_Type__c,Tenancy_Contract_Cancellation_Date__c , Total_Visas_Available__c,Tenancy_Contract_Start_Date__c, Tenancy_Contract_Expiry_Date__c, Rental_Rate__c, Name, Location_type__c, Location__c, Id From Tenancy_Contract__c where ((Tenancy_Contract_Cancellation_Date__c =: null) AND (Tenant__c =: accountId))])
                {
                    if(tenancyObject.Tenancy_Contract_Expiry_Date__c <= tenancyObject.Account_Expiry_Date__c)
                    {
                        //We Need to clone the existing Records and then insert....
                        Tenancy_Contract__c tempTenantObject = new Tenancy_Contract__c();
                        tempTenantObject = tenancyObject.clone(false,true);
                        
                        //Now Change the Temp Application Fee Type to Renewal...
                        tempTenantObject.Fee_Application_Type__c = 'Renewal';
                        tempTenantObject.RecordTypeId = tenancyContractRecordType.id;
                        
                        //For Setting the Start/Expiry Date of Tenancy Contract Logic...
                        if(tenancyObject.Account_Expiry_Date__c == (tenancyObject.Tenancy_Contract_Expiry_Date__c).addyears(1))
                        {
                            tempTenantObject.Tenancy_Contract_Start_Date__c = tenancyObject.Tenancy_Contract_Expiry_Date__c.adddays(1);
                            //tempTenantObject.Tenancy_Contract_Expiry_Date__c = (tenancyObject.Tenancy_Contract_Expiry_Date__c).addyears(1).adddays(-1);
                            tempTenantObject.Tenancy_Contract_Expiry_Date__c = (tenancyObject.Tenancy_Contract_Expiry_Date__c).addyears(1);
                            tempTenantObject.Rental_Rate__c = tenancyObject.Location__r.Rental_Rate__c;
                        }
                        //else if((tenancyObject.Tenancy_Contract_Expiry_Date__c < System.Today()) && (tenancyObject.Account_Today_Expiry_Date__c < 30))
                        else if((tenancyObject.Tenancy_Contract_Expiry_Date__c < System.Today()) && (Decimal.valueof(System.Today().daysbetween((tenancyObject.Account_Expiry_Date__c).addyears(-1).adddays(1) )).setScale(2) < 30))
                        {
                            tempTenantObject.Tenancy_Contract_Start_Date__c = tenancyObject.Tenancy_Contract_Expiry_Date__c.adddays(1);
                            tempTenantObject.Tenancy_Contract_Expiry_Date__c = (tenancyObject.Account_Expiry_Date__c);
                            Decimal daybetweenValue = Decimal.valueof(tenancyObject.Tenancy_Contract_Expiry_Date__c.daysbetween((tenancyObject.Account_Expiry_Date__c))).setScale(2);
                            tempTenantObject.Rental_Rate__c = ((tenancyObject.Location__r.Rental_Rate__c / 360) * (daybetweenValue ));
                            
                            
                        
                        }
                        else
                        {
                            tempTenantObject.Tenancy_Contract_Start_Date__c = tenancyObject.Tenancy_Contract_Expiry_Date__c.adddays(1);
                            tempTenantObject.Tenancy_Contract_Expiry_Date__c = (tenancyObject.Account_Expiry_Date__c);
                            Decimal daybetweenValue = Decimal.valueof(tenancyObject.Tenancy_Contract_Expiry_Date__c.daysbetween((tenancyObject.Account_Expiry_Date__c))).setScale(2);
                            tempTenantObject.Rental_Rate__c = ((tenancyObject.Location__r.Rental_Rate__c / 360) * (daybetweenValue));
                            
                            
                            
                        }
                        
                        
                        //for Updating the Previous Tenant Object....
                        tenancyObject.Tenancy_Contract_Cancellation_Date__c = date.Today();
                                            
                        //Adding in the Insertion List...
                        tenancyContractList.add(tempTenantObject);
                        
                        //Adding in the Updating List of Previous Records....
                        tenancyContractPreviousRecordList.add(tenancyObject);   
                        
                    }
                    else if(tenancyObject.Tenancy_Contract_Expiry_Date__c > tenancyObject.Account_Expiry_Date__c)//To Accomodate their messy data...
                    {
                        
                        //Putting in the Result List...
                        resultList.add(tenancyObject.Id);
                        
                    }
                    
                }*/
                //For Location BOoking....   
                for(Location_Booking_Information__c bookingObject : accountObject.Locations_Booking_Information__r)
                {
                    System.debug('The Location Booking Id =>'+ bookingObject.Id);
                    //Change the Location Leasing date and Status...
                    bookingObject.License_Booking_Status__c = 'Occupied';
                    bookingObject.Booking_Leasing_Issue_Date__c = system.Today();          
                    
                    //Adding in the list to update....
                    locationBookingList.add(bookingObject);    
                    
                }            
                if(tenancyContractList.size() > 0)
                {
                    //First Updating the Previous Version...
                    update tenancyContractPreviousRecordList;
                    
                    //First insert the tenancy contract...
                    insert tenancyContractList;
                    
                    //Updating the task...
                    update locationBookingList;
                    
                    //Now Update the Location Booking Selected Location Field (Current Tenancy Contract);
                    List<ID> LocationIDs = new List<ID>();
                    for(Tenancy_Contract__c tempTenancy : tenancyContractList)
                    {
                        //Putting in the Result List...
                        resultList.add(tempTenancy.Id);
                        LocationIDs.Add(tempTenancy.Location__c);
                    }
                    
                    //For Changing the current Tenancy Contract with the new one...
                    List<Location__c> LocationsList = [SELECT id, Current_Tenancy_Contract__c FROM Location__c WHERE id IN :LocationIDs];
                    
                    For(Tenancy_Contract__c tempTenancy : tenancyContractList)
                    {
                        For(Location__c Location : LocationsList)
                        {
                            if(tempTenancy.Location__c == Location.id)
                            {
                                Location.Current_Tenancy_Contract__c = tempTenancy.id;
                                break;
                            }
                        }
                    }
                    
                    //Updating the Location
                    update(LocationsList);
                    
                    
                    
                }
                /*else
                {
                    resultList.add('All the tenancy Records are cancelled !!!');
                    return resultList;
                    
                }*/
                
                if(resultList.size() > 0)
                {
                    //Now Updating the Tenancy Contract Flag on the COmpany...
                    accountObject.isTenancyCreated__c = true;
                    accountObject.License_Application_Step__c = 'Tenancy Contract Issued';
                    update accountObject;   
                }
                
            }
            
            //Returning the Ids...
            return resultList;
            
        }
        catch(System.Exception exp)
        {
            system.debug('The Exception is => '+ exp.getMessage()); 
            Database.rollback(sp);
            return null;
        }
    }
    
    //For Creating the Tenancy Contract Method For Renewal License...
    Webservice static List<string> creatingTenancyContractForRenewalLicenseForTenancyContractMethod(ID accountId,ID tenancyId)
    {
        Savepoint sp = Database.setSavepoint();
        try
        {
            Account accountObject = new Account();
            accountObject = [select Id, RecordTypeId,isTenancyCreated__c, License_Issue_Date__c,License_Application_Step__c,  License_Expiry_Date__c, License_State_Date__c,(Select Id, Account__c, License_Booking_Status__c, Location__c, Booking_Date__c, Booking_Leasing_Issue_Date__c, Booking_Released_Date__c,Location__r.Rental_Rate__c, Location__r.Name, Location__r.Area__c,Location__r.Area__r.Name, Location__r.Fee__c, Location__r.Area__r.Project_Type__c, Location__r.Current_Tenancy_Contract__c,Location__r.Current_Tenancy_Contract__r.Name, Location__r.Current_Tenancy_Contract__r.Contract_Status__c,Location__r.Current_Tenancy_Contract__r.Location_type__c, Location__r.Current_Tenancy_Contract__r.Rental_Rate__c,Location__r.Current_Tenancy_Contract__r.Tenancy_Contract_Expiry_Date__c, Location__r.Visa_Quota__c, Location__r.Current_Tenancy_Contract__r.Tenancy_Contract_Start_Date__c, Location__r.Current_Tenancy_Contract__r.Total_Visas_Available__c  From Locations_Booking_Information__r) from Account where Id =: accountId];
            List<Tenancy_Contract__c> tenancyContractList = new List<Tenancy_Contract__c>();
            List<Tenancy_Contract__c> tenancyContractPreviousRecordList = new List<Tenancy_Contract__c>();
            List<Location_Booking_Information__c> locationBookingList = new List<Location_Booking_Information__c>();
            list<String> resultList = new list<string>();
            
            RecordType tenancyContractRecordType = new RecordType();
            tenancyContractRecordType = [select id,SObjectType,DeveloperName,IsActive,Name from RecordType where SObjectType =: 'Tenancy_Contract__c' AND IsActive =: true AND DeveloperName =:'New_Tenancy_Contract'];  
            
            Map<ID,Invoice_Line_Item__c> invoiceItemMap = new Map<Id,Invoice_Line_Item__c>();
            
            
            if(accountObject != null)
            {
                //Invoice Always One...
                //for(Invoice__c invoiceObject : [Select Invoice_Type__c, Id, Fee_Application_Type__c, Cancelled__c, Account__c,CreatedDate, (Select Id, Name, Invoice__c, Amount__c, Fee__c, Location_Booking_Information__c,Location_Booking_Information__r.id, Location__c, Package_Value__c, Action__c, Installment__c,Tenancy_Contract__c,Tenancy_Contract__r.Id, Payment_Plan__c From Invoice_Line_Items__r WHERE Name =: 'Location Rental Rate') From Invoice__c WHERE Account__c =: accountId AND Invoice_Type__c =: 'Non Refundable' AND Fee_Application_Type__c =: 'Tenancy Renewal' AND Cancelled__c =: false ORDER BY CreatedDate DESC LIMIT 1])
                for(Invoice__c invoiceObject : [Select Invoice_Type__c, Id, Fee_Application_Type__c, Cancelled__c, Account__c,CreatedDate, (Select Id, Name,Start_Date__c,End_Date__c, Invoice__c, Amount__c, Fee__c, Location_Booking_Information__c,Location_Booking_Information__r.id, Location__c, Package_Value__c, Action__c, Installment__c,Tenancy_Contract__c,Tenancy_Contract__r.Id, Payment_Plan__c From Invoice_Line_Items__r WHERE Name Like: 'Location Rental Rate%') From Invoice__c WHERE Account__c =: accountId AND Invoice_Type__c =: 'Non Refundable' AND Fee_Application_Type__c =: 'Tenancy Renewal' AND Cancelled__c =: false ORDER BY CreatedDate DESC LIMIT 1])
                {
                    for(Invoice_Line_Item__c invoiceItem : invoiceObject.Invoice_Line_Items__r )
                    {
                        //Putting in the Map...
                        //invoiceItemMap.put(invoiceItem.Tenancy_Contract__r.id, invoiceItem);
                        invoiceItemMap.put(invoiceItem.Tenancy_Contract__r.id, invoiceItem);
                    }   
                }
                
                
                System.debug('For Tenancy Contract Above Location List Tenanct Id => '+ accountId + ' && The Tenancy Id => '+ tenancyId);
                
                for(Tenancy_Contract__c tenancyObject : [Select Tenant__c,Location__r.Rental_Rate__c,Tenancy_Contract_Renewal_Steps__c,Account_Today_Expiry_Date__c,Account_Expiry_Date__c,Fee_Application_Type__c,Tenancy_Contract_Cancellation_Date__c , Total_Visas_Available__c,Tenancy_Contract_Start_Date__c, Tenancy_Contract_Expiry_Date__c, Rental_Rate__c, Name, Location_type__c, Location__c, Id From Tenancy_Contract__c where ((Tenancy_Contract_Cancellation_Date__c =: null) AND (Tenant__c =: accountId) AND (Id =: tenancyId)) ])
                {
                    //We Need to clone the existing Records and then insert....
                    Tenancy_Contract__c tempTenantObject = new Tenancy_Contract__c();
                    tempTenantObject = tenancyObject.clone(false,true);
                    
                    //Now Change the Temp Application Fee Type to Renewal...
                    tempTenantObject.Fee_Application_Type__c = 'Renewal';
                    tempTenantObject.RecordTypeId = tenancyContractRecordType.Id;
                    System.debug('For Tenancy Contract After Contract');
                    tempTenantObject.Tenancy_Contract_Start_Date__c = tenancyObject.Tenancy_Contract_Expiry_Date__c.adddays(1);                    
                    tempTenantObject.Tenancy_Contract_Expiry_Date__c = tenancyObject.Account_Expiry_Date__c;
                    
                    //For Packagge System... (Renetal Rate Calculation)
                    if(invoiceItemMap.get(tenancyObject.Id) != null)
                    {
                        tempTenantObject.Rental_Rate__c =invoiceItemMap.get(tenancyObject.Id).Amount__c;
                        tempTenantObject.Package_Value__c = invoiceItemMap.get(tenancyObject.Id).Package_Value__c;
                        tempTenantObject.Payment_Plan__c = invoiceItemMap.get(tenancyObject.Id).Payment_Plan__c;
                        tempTenantObject.Action__c = invoiceItemMap.get(tenancyObject.Id).Action__c;
                        tempTenantObject.Installment__c = invoiceItemMap.get(tenancyObject.Id).Installment__c;
                        
                        //Adding on the 25th Nov 2013
                        tempTenantObject.Tenancy_Contract_Start_Date__c = invoiceItemMap.get(tenancyObject.Id).Start_Date__c;                    
                        tempTenantObject.Tenancy_Contract_Expiry_Date__c = invoiceItemMap.get(tenancyObject.Id).End_Date__c;
                    }
                    else
                    {
                        Decimal daysBetweenValue = Decimal.valueof((tenancyObject.Tenancy_Contract_Expiry_Date__c).daysbetween(tenancyObject.Account_Expiry_Date__c)).setScale(2);
                        tempTenantObject.Rental_Rate__c = ((tenancyObject.Location__r.Rental_Rate__c / 360) * (daysBetweenValue ));                             
                    }
                    
                    tempTenantObject.Tenancy_Contract_Renewal_Steps__c = 'Tenancy Contract Renewed';
                    
                    //for Updating the Previous Tenant Object....
                    tenancyObject.Tenancy_Contract_Cancellation_Date__c = date.Today();
                    tenancyObject.Confirm_Under_Renewal__c = true;
                    tenancyObject.Tenancy_Contract_Renewal_Steps__c = 'Tenancy Contract Renewed';
                             
                    System.debug('For Tenancy Contract Under List');         
                    //Adding in the Insertion List...
                    tenancyContractList.add(tempTenantObject);
                    
                    //Adding in the Updating List of Previous Records....
                    tenancyContractPreviousRecordList.add(tenancyObject);
                    
                    System.debug('For Tenancy Contract Previous List  => ' + tenancyContractPreviousRecordList.size());
                    
                }
                
                //Before 16th October 2012 .... (Before Package Implementation)...
                /*for(Tenancy_Contract__c tenancyObject : [Select Tenant__c,Location__r.Rental_Rate__c,Tenancy_Contract_Renewal_Steps__c,Account_Today_Expiry_Date__c,Account_Expiry_Date__c,Fee_Application_Type__c,Tenancy_Contract_Cancellation_Date__c , Total_Visas_Available__c,Tenancy_Contract_Start_Date__c, Tenancy_Contract_Expiry_Date__c, Rental_Rate__c, Name, Location_type__c, Location__c, Id From Tenancy_Contract__c where ((Tenancy_Contract_Cancellation_Date__c =: null) AND (Tenant__c =: accountId) AND (Id =: tenancyId)) ])
                {
                    //We Need to clone the existing Records and then insert....
                    Tenancy_Contract__c tempTenantObject = new Tenancy_Contract__c();
                    tempTenantObject = tenancyObject.clone(false,true);
                    
                    //Now Change the Temp Application Fee Type to Renewal...
                    tempTenantObject.Fee_Application_Type__c = 'Renewal';
                    tempTenantObject.RecordTypeId = tenancyContractRecordType.Id;
                    System.debug('For Tenancy Contract After Contract');
                    
                    
                    //tempTenantObject.Tenancy_Contract_Start_Date__c = tenancyObject.Tenancy_Contract_Expiry_Date__c.adddays(1);
                    
                    tempTenantObject.Tenancy_Contract_Start_Date__c = tenancyObject.Tenancy_Contract_Expiry_Date__c.adddays(1);                    
                    tempTenantObject.Tenancy_Contract_Expiry_Date__c = tenancyObject.Account_Expiry_Date__c;
                    //tempTenantObject.Rental_Rate__c = ((tenancyObject.Location__r.Rental_Rate__c / 360) * ((tenancyObject.Account_Expiry_Date__c).addyears(1)).daysbetween(System.today()) );
                    //Decimal daysBetweenValue = Decimal.valueof((tenancyObject.Account_Expiry_Date__c).addyears(1).daysbetween(System.today())).setScale(2);
                    Decimal daysBetweenValue = Decimal.valueof((tenancyObject.Tenancy_Contract_Expiry_Date__c).daysbetween(tenancyObject.Account_Expiry_Date__c)).setScale(2);
                    tempTenantObject.Rental_Rate__c = ((tenancyObject.Location__r.Rental_Rate__c / 360) * (daysBetweenValue ));
                    
                    
                    tempTenantObject.Tenancy_Contract_Renewal_Steps__c = 'Tenancy Contract Renewed';
                    
                    //for Updating the Previous Tenant Object....
                    tenancyObject.Tenancy_Contract_Cancellation_Date__c = date.Today();
                    tenancyObject.Confirm_Under_Renewal__c = true;
                    tenancyObject.Tenancy_Contract_Renewal_Steps__c = 'Tenancy Contract Renewed';
                             
                    System.debug('For Tenancy Contract Under List');         
                    //Adding in the Insertion List...
                    tenancyContractList.add(tempTenantObject);
                    
                    //Adding in the Updating List of Previous Records....
                    tenancyContractPreviousRecordList.add(tenancyObject);
                    
                    System.debug('For Tenancy Contract Previous List  => ' + tenancyContractPreviousRecordList.size());
                    
                }
                */
                
                //For Location BOoking....   
                for(Location_Booking_Information__c bookingObject : accountObject.Locations_Booking_Information__r)
                {
                    System.debug('The Location Booking Id =>'+ bookingObject.Id);
                    //Change the Location Leasing date and Status...
                    bookingObject.License_Booking_Status__c = 'Occupied';
                    bookingObject.Booking_Leasing_Issue_Date__c = system.Today();          
                    
                    System.debug('For Tenancy Contract Under Location List ');
                    //Adding in the list to update....
                    locationBookingList.add(bookingObject);    
                    
                }
                            
                if(tenancyContractList.size() > 0)
                {
                    //First Updating the Previous Version...
                    update tenancyContractPreviousRecordList;
                    
                    //First insert the tenancy contract...
                    insert tenancyContractList;
                    
                    //Updating the task...
                    update locationBookingList;
                    
                    //Now Update the Location Booking Selected Location Field (Current Tenancy Contract);
                    List<ID> LocationIDs = new List<ID>();
                    for(Tenancy_Contract__c tempTenancy : tenancyContractList)
                    {
                        //Putting in the Result List...
                        resultList.add(tempTenancy.Id);
                        LocationIDs.Add(tempTenancy.Location__c);
                    }
                    
                    //For Changing the current Tenancy Contract with the new one...
                    List<Location__c> LocationsList = [SELECT id, Current_Tenancy_Contract__c FROM Location__c WHERE id IN :LocationIDs];
                    
                    For(Tenancy_Contract__c tempTenancy : tenancyContractList)
                    {
                        For(Location__c Location : LocationsList)
                        {
                            if(tempTenancy.Location__c == Location.id)
                            {
                                Location.Current_Tenancy_Contract__c = tempTenancy.id;
                                break;
                            }
                        }
                    }
                            
                    //Updating the Location
                    update(LocationsList);
                    
                    System.debug('The Location Type List => '+ LocationsList);
                    
                    //Now Updating the Tenancy Contract Flag on the COmpany...
                    //accountObject.isTenancyCreated__c = true;
                    //update accountObject;
                    
                }
                
            }
            
            //Returning the Ids...
            return resultList;
            
        }
        catch(System.Exception exp)
        {
            system.debug('The Exception is => '+ exp.getMessage()); 
            Database.rollback(sp);
            return null;
        }
    }

}